Thread overview
foreach () processing sequence
Feb 06, 2007
jicman
Feb 06, 2007
Kirk McDonald
Feb 06, 2007
jicman
Feb 06, 2007
Johan Granberg
Feb 06, 2007
jicman
Feb 06, 2007
Carlos Santander
February 06, 2007
Greetings!

Imagine this declarion,

char[] str = ["bb", "cc", "aa", "00", "11", "zz", "dd"];

when I do a,

foreach (char[] s; str)
  writefln(s);

assuming that the str array has not been touched or altered in any way, will the sequence of execution **ALWAYS** follow the sequence of the array creation?  In other words, will the execution of the foreach above always display,

bb
cc
aa
00
11
zz
dd

Thanks,

josé
February 06, 2007
jicman wrote:
> Greetings!
> 
> Imagine this declarion,
> 
> char[] str = ["bb", "cc", "aa", "00", "11", "zz", "dd"];
> 
> when I do a,
> 
> foreach (char[] s; str)
>   writefln(s);
> 
> assuming that the str array has not been touched or altered in any
> way, will the sequence of execution **ALWAYS** follow the sequence
> of the array creation?  In other words, will the execution of the
> foreach above always display,
> 
> bb
> cc
> aa
> 00
> 11
> zz
> dd
> 
> Thanks,
> 
> jos�

(Nitpick: The type of str is char[][].)

Yes. An array is an ordered sequence. (As opposed to, say, a hash table, which is unordered.)

-- 
Kirk McDonald
Pyd: Wrapping Python with D
http://pyd.dsource.org
February 06, 2007
== Quote from Kirk McDonald's article
> jicman wrote:
> > Greetings!
> >
> > Imagine this declarion,
> >
> > char[] str = ["bb", "cc", "aa", "00", "11", "zz", "dd"];
> >
> > when I do a,
> >
> > foreach (char[] s; str)
> >   writefln(s);
> >
> > assuming that the str array has not been touched or altered in
any
> > way, will the sequence of execution **ALWAYS** follow the
sequence
> > of the array creation?  In other words, will the execution of the foreach above always display,
> >
> > bb
> > cc
> > aa
> > 00
> > 11
> > zz
> > dd
> >
> > Thanks,
> >
> > jos�
> (Nitpick: The type of str is char[][].)
> Yes. An array is an ordered sequence. (As opposed to, say, a hash
table,
> which is unordered.)

You're not nitpick, but acurate. :-)

Ok, thanks.

February 06, 2007
jicman wrote:

> == Quote from Kirk McDonald's article
>> jicman wrote:
>> > Greetings!
>> >
>> > Imagine this declarion,
>> >
>> > char[] str = ["bb", "cc", "aa", "00", "11", "zz", "dd"];
>> >
>> > when I do a,
>> >
>> > foreach (char[] s; str)
>> >   writefln(s);
>> >
>> > assuming that the str array has not been touched or altered in
> any
>> > way, will the sequence of execution **ALWAYS** follow the
> sequence
>> > of the array creation?  In other words, will the execution of the foreach above always display,
>> >
>> > bb
>> > cc
>> > aa
>> > 00
>> > 11
>> > zz
>> > dd
>> >
>> > Thanks,
>> >
>> > jos�
>> (Nitpick: The type of str is char[][].)
>> Yes. An array is an ordered sequence. (As opposed to, say, a hash
> table,
>> which is unordered.)
> 
> You're not nitpick, but acurate. :-)
> 
> Ok, thanks.

I have wondered the same in the past. Is it documented somewhere? otherwise I think it should be.
February 06, 2007
== Quote from Johan Granberg's article
> jicman wrote:
> > == Quote from Kirk McDonald's article
> >> jicman wrote:
> >> > Greetings!
> >> >
> >> > Imagine this declarion,
> >> >
> >> > char[] str = ["bb", "cc", "aa", "00", "11", "zz", "dd"];
> >> >
> >> > when I do a,
> >> >
> >> > foreach (char[] s; str)
> >> >   writefln(s);
> >> >
> >> > assuming that the str array has not been touched or altered in
> > any
> >> > way, will the sequence of execution **ALWAYS** follow the
> > sequence
> >> > of the array creation?  In other words, will the execution of
the
> >> > foreach above always display,
> >> >
> >> > bb
> >> > cc
> >> > aa
> >> > 00
> >> > 11
> >> > zz
> >> > dd
> >> >
> >> > Thanks,
> >> >
> >> > jos�
> >> (Nitpick: The type of str is char[][].)
> >> Yes. An array is an ordered sequence. (As opposed to, say, a
hash
> > table,
> >> which is unordered.)
> >
> > You're not nitpick, but acurate. :-)
> >
> > Ok, thanks.
> I have wondered the same in the past. Is it documented somewhere?
otherwise
> I think it should be.

I agree.  I was just going to write a function to make sure that the the creation sequence was the sequence that it would execute on a foreach().
February 06, 2007
jicman escribió:
> == Quote from Johan Granberg's article
>> jicman wrote:
>>> == Quote from Kirk McDonald's article
>>>> jicman wrote:
>>>>> Greetings!
>>>>> 
>>>>> Imagine this declarion,
>>>>> 
>>>>> char[] str = ["bb", "cc", "aa", "00", "11", "zz", "dd"];
>>>>> 
>>>>> when I do a,
>>>>> 
>>>>> foreach (char[] s; str) writefln(s);
>>>>> 
>>>>> assuming that the str array has not been touched or altered in
>>> any
>>>>> way, will the sequence of execution **ALWAYS** follow the
>>> sequence
>>>>> of the array creation?  In other words, will the execution of
> the
>>>>> foreach above always display,
>>>>> 
>>>>> bb cc aa 00 11 zz dd
>>>>> 
>>>>> Thanks,
>>>>> 
>>>>> jos�
>>>> (Nitpick: The type of str is char[][].) Yes. An array is an ordered
>>>> sequence. (As opposed to, say, a
> hash
>>> table,
>>>> which is unordered.)
>>> You're not nitpick, but acurate. :-)
>>> 
>>> Ok, thanks.
>> I have wondered the same in the past. Is it documented somewhere?
> otherwise
>> I think it should be.
> 
> I agree.  I was just going to write a function to make sure that the the
> creation sequence was the sequence that it would execute on a foreach().

Check http://www.digitalmars.com/d/statement.html#ForeachStatement :
> For foreach, the elements for the array are iterated over starting at index 0
> and continuing to the maximum of the array. For foreach_reverse, the array
> elements are visited in the reverse order.


-- 
Carlos Santander Bernal