Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Where does perl document, or otherwise quarantee, that foreach (@somelist) will iterate over the list in strict first to last element order? (PHM)

Replies are listed 'Best First'.
Re: Iteration order of a foreach
by dragonchild (Archbishop) on Nov 07, 2007 at 19:57 UTC
    In the documentation - the key is "in turn"

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
      Thank you, Dragonchild. It's nice to know my scripts work because they follow the language definition, rather than because they make the same mistake everybody else makes. Still, I can't help feeling, that if the page writers had spend less time demonstrating that Perl is ill-suited for C-style programming, they might have found the time to instead come up with something more explicite.

      Anyway, nearly all my questions on Perl design solved.
      Thanks again. (PHM)
Re: Iteration order of a foreach
by ikegami (Patriarch) on Nov 07, 2007 at 20:36 UTC
    It does it in order, and that's not something that's going to change in Perl5 because that would break just about every script that uses foreach.
Re: Iteration order of a foreach
by aquarium (Curate) on Nov 07, 2007 at 22:04 UTC
    it is the array in list context that produces the list elements in order...by definition an array is an ordered list...and there isn't any better place to start popping the array than the start.
    you can get different behavior if you use a hash or function instead
    the hardest line to type correctly is: stty erase ^H

      The thing is, I think for (@array) is optimized to take a reference to the array rather putting it in list context (similarly to how for (x..y) is optimized not to flatten the list).

      sub f4 { for (@_) { print($_); shift; } print("\n"); } sub f5 { for ((), @_) { print($_); shift; } print("\n"); } f4(map/./g, Japh); # Jp f5(map/./g, Japh); # Japh

      (Copied from Re^3: For vs. While)

      I believe there are 5 kinds of for loops.

      • for (x; y; z) (C-style syntax)
      • for (x..y) (Not flattened)
      • for (ARRAY) (Not flattened)
      • for (reverse LIST) (Flattened, but no explicit call to reverse)
      • for (LIST)
Re: Iteration order of a foreach
by ambrus (Abbot) on Nov 08, 2007 at 08:58 UTC

    Perldoc perlsyn says

    The "foreach" keyword is actually a synonym for the "for" keyword, so you can use "foreach" for readability or "for" for brevity. (Or because the Bourne shell is more familiar to you than csh, so writing "for" comes more naturally.) If VAR is omitted, $_ is set to each value.
    As the for statement of the shells iterate over a list of words in order, I guess this guarantees the same for perl because it wants to be compatible. (Otoh awk has a for statement that iterates over an array in an undefined order, and perl is also awk-compatible, so you can't be sure.)