Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: How do I Loop over multiple arrays simultaneously ?

by simmisam (Novice)
on Jan 15, 2014 at 06:32 UTC ( [id://1070649]=note: print w/replies, xml ) Need Help??


in reply to How do I Loop over multiple arrays simultaneously ?

Below code will take array of any length and will give the desired output.

my @array1 = ('a','b','c','d'); my @array2 = (1,2,3,4); my @array3 = ('x','y','z'); my @array4 = ('p','q','r','s','t','u'); my @array5 = ('l','m','n'); my @finalArray; looping(\@array1,\@array2,\@array3,\@array4,\@array5); sub looping { while (@_) { my $array = shift; my $length = scalar @{$array} - 1; for my $i (0 .. $length) { exists $finalArray[$i] ? ($finalArray[$i] = $finalArray[$i +].${$array}[$i]) : ($finalArray[$i] = ${$array}[$i]); } } } print "$_\n" foreach(@finalArray);

Replies are listed 'Best First'.
Re: Answer: How do I Loop over multiple arrays simultaneously ?
by dcmertens (Scribe) on Jan 16, 2014 at 02:32 UTC
    Due to the string concatenation, this doesn't work if my arrays have nested data structures.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2026-03-12 07:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.