Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking if an array index IsDefined

Status
Not open for further replies.

jl8789

MIS
May 22, 2003
293
US
I pulled this note right out of the help file of coldfusion:

Note: The IsDefined function does not test the existence of array elements. To test whether data exists at an array index, copy the array element to a simple variable and use the IsDefined function to test the existence of the copy.

Well, what am I doing wrong then? Here is my code:
<cfset exceptArray = exceptions[#thisday#][1]>
<cfif IsDefined(&quot;exceptArray&quot;)>
<cfif #exceptArray# eq #CurrentDay#>
#CurrentDay#
</cfif>
</cfif>

There are only 2 sets of data in this array, [1][1], [2][1].
So when it looks for the third set it should skip the code and continue processing, except I receive this error message:
The element at position 1, of dimension 2, of an array object used as part of an expression, cannot be found.

How do I test if [3][1] exists, and if not, just continue?
 
CFMX sucks..

ParameterExists is deprecated? That stinks, I don't use it but the site I now work on uses it extensively..

Anyway as for your problem, you might just create a list..

<Cfset arrlist=&quot;&quot;>

And when you create a new array element, append it to the list, that way you know what elements exists.. and then you say..

<cfif listfind(arrList,thisDay)>
...code...
</cfif>

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
I ran into this problem myself today. The only solution that I've found is the following:

<cfset x = ArrayNew(1)>
<cfset x[1] = &quot;A&quot;>
<cfset x[3] = &quot;C&quot;>
<cfset x[4] = &quot;D&quot;>
<cfset pos = 2>
<cftry>
<cfset Element_Exists = &quot;Yes&quot;>
<cfset Var_Value = x[Pos]>
<cfcatch type=&quot;Any&quot;>
<cfset Element_Exists = &quot;No&quot;>
</cfcatch>
</cftry>

Hope this helps...
 
Also.. you could use regular variables...

<cfset exceptArray = exceptions[#thisday#][1]>

would be

<cfset exceptArray = exceptions_#thisday#_1>

rather cheap but then you could use variable validation functions.

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
er: <cfset exceptArray = evaluate(exceptions_#thisday#_1)>

Sorry... duh..

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
I think I'll just quit programming now..

<cfset exceptArray = evaluate(&quot;exceptions_#thisday#_1&quot;)>

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Ok I think I see what you need, not as complex as it seems.

Your code:
<cfset exceptArray = exceptions[#thisday#][1]>
<cfif IsDefined(&quot;exceptArray&quot;)>
<cfif #exceptArray# eq #CurrentDay#>
#CurrentDay#
</cfif>
</cfif>

Assuming exceptions[][] exists -- My code:
<CFLOOP from=&quot;1&quot; to=&quot;#arrayLen(exceptions)#&quot; index=&quot;i&quot;>
<CFIF exceptions[1] is currentDay>
#currentDay#<br>
</CFIF>
</CFLOOP>

hope this helps a bit
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top