Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: can we pass two variable to foreach loop

by Zaxo (Archbishop)
on Mar 28, 2007 at 05:57 UTC ( #606916=note: print w/ replies, xml ) Need Help??


in reply to can we pass two variable to foreach loop

Not exactly. You can set up that kind of thing with Perl's C-style for loop. Perl style for like you show takes single element at a time from a list.

If you can get several hunks of data into each element of the list, you can define ($x,$y) inside the loop and extract the data:

for ([qw/Joe Blow/], [qw/John Doe/]) { my ($x, $y) = @$_; # do something }
or
for ('Joe Blow', 'John Doe') { my ($x, $y) = split; # and so on }

The for loop in your example could be written:

{ my ($x, $y) = ($name, $lastname); # do something }

C-style for can get very baroque, but that's not encouraged, even in C.

After Compline,
Zaxo


Comment on Re: can we pass two variable to foreach loop
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (11)
As of 2015-07-29 12:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (263 votes), past polls