Does For Each...Next always iterate through an array in the same order
(increasing from the first element to the last)?

Brian

Re: For Each by gs

gs
Wed Aug 16 11:18:08 CDT 2006

If I remember correctly, that is undocumented. I would not rely on it as it
may be undocumented behaviour.

you may want to use for loop with index if you want to control the order
every single time

<brian.lukoff@gmail.com> wrote in message
news:1155742691.443502.276910@i3g2000cwc.googlegroups.com...
> Does For Each...Next always iterate through an array in the same order
> (increasing from the first element to the last)?
>
> Brian
>



Re: For Each by Dan

Dan
Wed Aug 16 11:22:15 CDT 2006

For Each will enumerate the collection from lowest to highest index
location each time used. A For loop to enumerate an array will be
based on however you set it up (forwards, backwards, stepped). The
members of a collection or the elements of an array are populated by
the creating process. Unless you are the controlling process, it is
safe to never assume that data returned to you will be in the exact
same order every time.

Re: For Each by brian

brian
Wed Aug 16 11:39:16 CDT 2006

Is the source of this possible differences in the ordering the result
of the fact that elements of the array or collection could be
rearranged? In a situation where we know that this is not the case,
like if an array comes from the result of a Split command, will that
array always be enumerated in increasing order (in this example, from
left to right in the string)?

Brian

Dan wrote:
> For Each will enumerate the collection from lowest to highest index
> location each time used. A For loop to enumerate an array will be
> based on however you set it up (forwards, backwards, stepped). The
> members of a collection or the elements of an array are populated by
> the creating process. Unless you are the controlling process, it is
> safe to never assume that data returned to you will be in the exact
> same order every time.


Re: For Each by Dan

Dan
Wed Aug 16 15:17:21 CDT 2006

If you are the source of the array data, then you have control in which
order the elements will be added to the array. Example, if I split
"Aa", "Bb", "Cc", "Aa" will always be index 0, "Bb" will always be
index 1, etc. How you crawl the array is under your control; if you
don't crawl it from index 0 to the upper bound index, then you wont
have data returned as expected. If, however, you are not in control of
the mechanism that provides you with the data (like you use a SQL, WMI,
LDAP, etc. or whatever as a source), then your script has no control
over how the data is delivered.

Re: For Each by brian

brian
Wed Aug 16 19:13:01 CDT 2006

I'm a little confused by what you've said--does this mean that this
code will always traverse the elements of the string in forwards order
(a to e) and print "abcde"?

x = Split("a,b,c,d,e", ",")
For Each c In x
Response.Write c
Next

And similarly, if x comes from a routine where I know how the elements
of the collection or array are ordered, then For Each will traverse the
elements in order? So in other words, these two are always equivalent
for an array x?

For i = 0 To UBound(x)
elt = x(i)
...
Next

For Each elt In x
...
Next

Dan wrote:
> If you are the source of the array data, then you have control in which
> order the elements will be added to the array. Example, if I split
> "Aa", "Bb", "Cc", "Aa" will always be index 0, "Bb" will always be
> index 1, etc. How you crawl the array is under your control; if you
> don't crawl it from index 0 to the upper bound index, then you wont
> have data returned as expected. If, however, you are not in control of
> the mechanism that provides you with the data (like you use a SQL, WMI,
> LDAP, etc. or whatever as a source), then your script has no control
> over how the data is delivered.


Re: For Each by Dan

Dan
Thu Aug 17 10:01:12 CDT 2006

The below code sample will always return the exact same output because
I populated array "X" with specific data in a specific order. The
First and Third routines are the exact same thing, although the first
gives you better control and is the standard way of accessing array
elements. The third method is the standard way of accessing collection

X = Split("A,B,C,D,E,F,G", ",")
'First
For I = 0 To UBound(X)-1
WScript.Echo "Index " & I & ": " & X(I)
Next
WScript.Echo
'Second
For I = UBound(X)-1 To 0 Step -2
WScript.Echo "Index " & I & ": " & X(I)
Next
WScript.Echo
'Third
For Each c In X
WScript.Echo c
Next

If you populate an array with an outside function that you know will
always return (for example) First Name, Last Name, Address in that
order, then sure, you can count on the order returned by the For Each
to be from index 0 to the upperbound index. You can also then use the
first routine to better control access.