> -----Original Message-----
> From: Jeffl [mailto:[EMAIL PROTECTED]]
> Sent: Friday, September 07, 2001 11:47 AM
> To: Bob Showalter
> Cc: 'beginners @ perl . org'
> Subject: RE: foreach and the ordering of array's
>
>
> when you run a foreach on a hash you have no idea what order
> you will get
> the keys unless
> you sort it, then you can only get it in the order defined by
> sort. If you
> use Tie::IxHash
> on a hash you can print out the has in the order it was constructed.
>
> Basically, I couldn't remeber what the Camel said about foreach and
> accessing an array.
> I went back and found this, which ties into what I had
> thought...(quote
> from the camel v.3
> pg.118) "There is no way with foreach to tell where you are
> in a list. You
> may compare adjacent elements by remembering the perevious one in a
> variable, but sometimes you just have to break down and write
> a three-part
> for loop with subscripts."
>
> So basically, what I get out of this, is that you don't know
> the order that
> foreach will retrieve the elements of the '@'. Now I could be mistaken
> here, and am not seeing the forest for the trees, and I
> should have just
> run a test to find out for myself.....I'm off to go build an
> @ and find out
> the order it parses through in a foreach, which I should have
> done in the
> first place......doh.....It was late......cut me some slack.
Some confusion in the terminology here.
for/foreach does not "retrieve" anything. It iterates over a list.
It will iterate over that list in the exact order the list elements
are supplied to for/foreach. Guaranteed.
One way to supply a list to for/foreach is to use the keys() function
to retrieve a list of the keys in a hash. keys() does not return the
key values in any particular order, due to the internal representation
of a hash.
But you want to supply an array to for/foreach. In that case, the
elements of the array are supplied in the array index sequence, which is
what you are looking for. This holds for any list context where an array
is used, not just for/foreach (e.g. print(), join(), map(), etc.)
(Inside the body of a for/foreach loop, you cannot determine the subscript
of the current array element you are processing. This is obvious, because
there is nothing that says the list supplied to for/foreach came from a
single array in the first place. But that doesn't mean that the loop does
not interate over the list in a guaranteed sequence.)
I'm sure others can explain this better than I can. But you are on the
right track to construct a test to confirm the behavior.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]