Check if a ColdFusion Array Position Exists

This is some sample script that I used today to check if a position in an array exists.  Basically, I was tasked with creating an email that showed hours from two separate datasources, and I needed to combine those side by side in an array to show the differences.  I knew that the first set of hours would always exist, so I looped over the second set of hours with a nested loop matching up the dates.  To check if the second position existed, I used a cfparam with set values of zero.  I did this because ColdFusion doesn't seem to have clean way of checking for a missing element.

<cfloop from="1" to="#arrayLen(totalArr)#" index="a">
            <cfset mytotals = structNew()/>
            <cfset mytotals["date"] = totalDate[a] />
            <cfset mytotals["hours"] = totalArr[a] />

<cfif arrayLen(totalUni) >
                <cfloop from="1" to="#arrayLen(totalUni)#" index="hh">
                <cfparam name="mytotals.unihours" type="string" default="0"/>
                    <cfif trim(totalDateUni[hh]) EQ trim(totalDate[a]) >
                        <cfset mytotals["unihours"] = totalUni[hh] />
                    </cfif>
                </cfloop>
            <cfelse>
                <cfset mytotals["unihours"] = 0 />
            </cfif>

<cfset arrayappend(myRetArray, mytotals) />
       </cfloop>

If there is a better way to do this, I would love to know how.

8 responses so far ↓

radekg - Apr 10, 2009 at 8:03 PM

Can't test as I don't have CF installed but generally .contains(Object elem) is your friend here:

<cfloop from="1" to="#arrayLen(totalArr)#" index="a">
<cfset mytotals = { date=totalDate[a], hours=totalArr[a], unihours=0 } />
<cfif arrayLen(totalUni) gt 0>
<cfif totalUni.contains( totalDate[a] )>
<cfset mytotals["unihours"] = totalUni[hh] />
</cfif>
</cfif>
<cfset arrayappend(myRetArray, mytotals) />
</cfloop>

or if that is not CF8:

<cfloop from="1" to="#arrayLen(totalArr)#" index="a">
<cfset mytotals = structNew() />
<cfset mytotals.date=totalDate[a] />
<cfset mytotals.hours=totalArr[a] />
<cfset mytotals.unihours=0 />
<cfif arrayLen(totalUni) gt 0>
<cfif totalUni.contains( totalDate[a] )>
<cfset mytotals["unihours"] = totalUni[hh] />
</cfif>
</cfif>
<cfset arrayappend(myRetArray, mytotals) />
</cfloop>

You definitely don't need inner else condition.

Stephen Weyrick - Apr 10, 2009 at 10:24 PM

Huh,

@radekg

I will have to try that. Thanks for your input.

Alex - Apr 21, 2009 at 8:01 AM

<cffunction name="Array_elementIsDefined" output="no">
<!--- dimensions: 1 --->
<cfargument name="array" type="array" required="true"/>
<cfargument name="index" type="numeric" required="true"/>
<cfset elementIsDefined=true/>
<cftry>
<cfset element=array[index]/>
<cfcatch>
<cfset elementIsDefined=false/>
</cfcatch>
</cftry>
<cfreturn elementIsDefined/>
</cffunction>

Stephen Weyrick - Apr 21, 2009 at 10:13 AM

@Alex

I tried to do it this way first. It worked, but it seemed kind of hackish. Cfparam seems a little bit cleaner, but I don't really like it either. There should be an easier way to do this: see the post above. I haven't tried radekg's method; I'm not sure radekg and I are talking about the same thing.

Henry Ho - Apr 22, 2009 at 1:02 PM

"ColdFusion doesn't seem to have clean way of checking for a missing element."?

ArrayIsDefined()

Description: Determines whether an array element is defined.

Returns: True, if the array element is defined (exists); false, otherwise.

History: ColdFusion 8: Added this function.

http://www.cfquickdocs.com/cf8/#ArrayIsDefined

Henry Ho - Apr 22, 2009 at 1:10 PM

To do an ArrayFind(), the cleanest but not really efficient way is to use

ArrayToList(), and ListFind()

I would not recommend using .contains() or any other underlying java methods. They tends to work sometime, but not all the time for me.

Stephen Weyrick - Apr 22, 2009 at 2:32 PM

@Henry

Thanks for your input, I will have to try that the next time I have to do something like this. It seems like a much better way of handling the problem.

falconseye - May 19, 2009 at 2:50 PM

Just found your blog via google. This is what I use: <cfset myArr = arrayNew(1)>

<cfloop from="1" to="10" step="2" index="i">
<cfset myArr[i] = 'aaa'>
</cfloop>
<cfdump var="#myArr#" label="before">

<cfoutput>
<cfloop from="1" to="10" index="j">
   <cftry>
   <cfset temp = len(myArr[j])>
   <cfcatch>
      <cfset myArr[j] = 'null'>
   </cfcatch>
   </cftry>
</cfloop>
</cfoutput>
<cfdump var="#myArr#" label="after">

Leave a Comment