Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Using an array element as a loop iterator

by GotToBTru (Prior)
on Nov 07, 2013 at 19:59 UTC ( [id://1061616]=note: print w/replies, xml ) Need Help??


in reply to Using an array element as a loop iterator

Not doing a very good job of reading the OP, people.

use strict; use warnings; my ($a); my @b=(2,4,6,7); foreach $a (@b) { print $a}

That works. What if I want to use one element of an array as the iterator? After all, $a[1] is really just a scalar, right?

use strict; use warnings; my @a=(1,2,3); my @b=(2,4,6,7); foreach $a[1] (@b) { print $a[1] }

Syntax error though, "near $a[". My guess is perl expects a scalar variable here, and $a[1] isn't a valid scalar variable name, even though it represents a scalar value.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1061616]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-16 13:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found