good chemistry is complicated,and a little bit messy -LW PerlMonks

### Re: Using an array element as a loop iterator

by Eily (Prior)
 on Nov 07, 2013 at 18:15 UTC ( #1061609=note: print w/replies, xml ) Need Help??

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 :

```@a = (1,2,3);
@b = (2,4,6,7);
print for @b[@a]; # print for elements of @b with indexes in @a
Or loop through @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
Could you write what you were intending to in python? If I translate your code it would be something like:
```a = [1,2,3]
b = [10,11,12,13]
for a[1] in b:
print a[1]

That prints all four elements of b

```10
11
12
13
and then a contains : [1, 13, 3]. So it does not correspond to your C-style for loop where you only print the first three elements, and the second value in the declaration of a is just thrown away.

To achieve the same result in Perl you can do:

```@a = 1..3;
@b = qw/2 4 7 9/;
for (@b) {\$a[1] = \$_; say; }

Thanks. That is nice

```for (@b){\$a[1]=\$_;say:}

But what I actually wanted to achieve was that I had several nested foreach loops and taking several variables, one for each iterator, is nearly horrible. I wanted to initialize one array and then use that as a container for all the iterators of nested arrays.

This step \$a[1]=\$_ or something similar will do that, but still that statement needs to be put as first step in loops of all 20 nested arrays. Isn't there any way through which we can avoid that and achieve the result via the foreach step only like something --

```my @a=();
foreach \$a[0](`cat file`){
foreach \$a[1](@b){
foreach \$a[2](grep (/4/, @c)){
<More statements>
}}}

And not using something like--

```my @a=();
foreach (`cat file`){
\$a[0]=\$_;
foreach \$a[1](@b){
\$a[1]\$_;
foreach \$a[2](grep (/4/, @c)){
\$a[2]=\$_
<More statements>
}}}
</code>

Create A New User
Node Status?
node history
Node Type: note [id://1061609]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2018-04-20 14:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My travels bear the most uncanny semblance to ...

Results (77 votes). Check out past polls.

Notices?