in reply to Re: Lists and Arrays and Boredom
in thread Lists and Arrays and Boredom

Thanks for your comments. The source text was a recent FAQ posting in comp.lang.perl.misc. The maintainers will be interested in your thoughts.
There are many different for loops. Some don't create a list (such as for (x..y) and for (@array)), and one iterates over an array (for (@array))
Isn't the array evaluated in list context and the for loop iterated over a list?

Replies are listed 'Best First'.
Re^3: Lists and Arrays and Boredom
by ikegami (Patriarch) on May 17, 2008 at 20:59 UTC

    Isn't the array evaluated in list context and the for loop iterated over a list?

    No, it's optimized. You'll notice if you add elements to the array from within the loop, the loop will iterate over them too. That's wouldn't be possible if the array had been flattened to a list (as it is in the second program below) since changing the array would have no effect on the list.

    >perl -le"@a=qw(a b); for (@a) { push @a, 'c' if /a/; print }" a b c >perl -le"@a=qw(a b); for (@a,()) { push @a, 'c' if /a/; print }" a b

    The maintainers will be interested in your thoughts.

    I might do that for "put in context" vs "evaluated in context". The rest of the comments don't apply to the FAQ. You made bad assumptions that if the FAQ mentions something about lists, it can't be the case for arrays as well (and vice-versa).

      Thank you for your explanation.
      You made bad assumptions that if the FAQ mentions something about lists, it can't be the case for arrays as well (and vice-versa).
      I did not make such an assumption at all.
        For example, you assumed that a list can't be initialized with a list.