in reply to Iteration order of a foreach

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

Replies are listed 'Best First'.
Re^2: Iteration order of a foreach
by ikegami (Patriarch) on Nov 08, 2007 at 03:26 UTC

    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)