in reply to Using an array element as a loop iterator
I suppose you didn't write what you thought you were writing. First, square brackets are not used to make a list, like in python, but an arrayref, which is only one element. So @a contains only one element which is [1,2,3]. @b on the other is indeed an array.
Could you write what you were intending to in python? If I translate your code it would be something like:
for each element in @b $a[1] = element print $a[1] done
If you want to use the indexes in @a to go through @b you can either use a slice (EDIT: not splice ...) of @b :
Or loop through @a :@a = (1,2,3); @b = (2,4,6,7); print for @b[@a]; # print for elements of @b with indexes in @a
@a = (1,2,3); @b = (2,4,6,7); for $i (@a) { print $b[$i]; }
NB: As for the error, I guess in for VAR (LIST) VAR can only be a variable, not any lvalue.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Using an array element as a loop iterator
by gurpreetsingh13 (Scribe) on Nov 08, 2013 at 03:56 UTC | |
by Eily (Monsignor) on Nov 08, 2013 at 10:21 UTC | |
by gurpreetsingh13 (Scribe) on Nov 08, 2013 at 15:17 UTC | |
by AnomalousMonk (Archbishop) on Nov 08, 2013 at 16:27 UTC | |
by Eily (Monsignor) on Nov 08, 2013 at 17:14 UTC | |
by LanX (Saint) on Nov 08, 2013 at 19:45 UTC | |
|