in reply to array element as foreach iterator

It's consistent with the documentation, which says a VAR is expected (with an optional my). $y[0] is an expression, not a variable. I suppose it could be considered a limitation, but not a bug.

The following can be used. If you want to next an arbitrary number of loops, check out Algorithm::Loops.

foreach $y0 (@x) { foreach $y1 (@x) { do_something($y0, $y1); } }

Replies are listed 'Best First'.
Re^2: array element as foreach iterator
by nobull (Friar) on Jan 27, 2005 at 18:33 UTC
    It's consistent with the documentation, which says a VAR is expected (with an optional my).

    It should be pointed out that if the variable is already declared lexical in an enclosing scope then the my is assumed. i.e. it is not the previously declared lexical that is used but a different one.

    IMNSHO perl should throw a warning when it infers an implicit my.

Re^2: array element as foreach iterator
by kingm61 (Initiate) on Jan 27, 2005 at 17:14 UTC
    OK, that explains why. I must use simple scalar variables (or the default $_) as a for/foreach iterator. Thanks.