You may not know this - but you can set array elements out of order...
<cfset a = arrayNew(1)>
<cfset a[1] = "foo">
<cfset a[3] = "foo">
This will create an array that has items at positions 1 and 3. However, if you try to loop over it using cfloop - you will get an error when you try to use item 2. You may think that isDefined() would work - but it does not. There is no one function that will determine if an array item exists. The only real solution is to use try/catch when you use the item. An MX only UDF for this can be found here:
http://cflib.org/udf.cfm?ID=632
However - one should not use the position of an array to signify something. If the position is important, consider reworking your data to be a structure. With structures, you can easily check for the existence of an item by using structKeyExists().
Archived Comments
I''m interested by your comment that suggests the index of an array should not have any signifance. Surely this is what differentiates an array from a structure: the order (read: "the INDEX") of the keys *is* significant in an array, and *not* in a structure (vagaries of how CF handles the presentation of structure keys aside).
I''m doubly interested by your comment that starts "If the position is important...". To me this idea is counterintuitive, and indeed flies in the face of pretty much everything I''ve read about arrays vis-a-vis structures.
Could you expand on what you mean?
Correct - the order of an array is important, but not the position. That''s why I would not use an array with data at positions 1 and 3. My argument comes down to - if you simply want ordered data, use the array, but if you want data that has a particular position, ray at 9, jacob at 3, etc, use a strtucture.
Aha! Gotcha. Good idea ;-)
hey