Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: Using an array element as a loop iterator

by Eily (Vicar)
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> }}}

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1061609]
Discipulus manual work: i just tell the same to my boss: every time the quick solution is to assign some manual data entry task to my group.. because we have not direct access to many databases here..
[LanX]: point is: in high speed trade each bank has to remember what he has to get from the others... so dresdner got billed for losses but couldn't claim gains
Discipulus is this the IT?
[Corion]: Discipulus: Well, in many cases it doesn't make sense to build an interface and complicated program just to enter 20 rows into a database ;) But yes, automating data imports should pay off in the long run
[LanX]: Choroba: this happened before I joined, was still in uni, but my boss was summoned to the CEO of the second biggest German bank at that time and could only say " I told them its not ready" ;)
[LanX]: memories....I missed my connection while chatting
[Discipulus]: in this case Corion we are speaking about software licensing: evry year or two we must rescan the whole ced to produce an excel report, while at every activation / disactivation we update a black box DB: i said that i a week i can produce the perl to..
[Discipulus]: rend out the xls IF i have access to the DB

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (9)
As of 2017-03-29 12:02 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (350 votes). Check out past polls.