http://www.perlmonks.org?node_id=825340

Fox has asked for the wisdom of the Perl Monks concerning the following question: (arrays)

Taken from RosettaCode...

How do I loop over multiple arrays and print the ith element of each?

For example, looping over the arrays (a,b,c), (A,B,C) and (1,2,3) to produce the output:

aA1 bB2 cC3

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I Loop over multiple arrays simultaneously ?
by Fox (Pilgrim) on Feb 25, 2010 at 16:56 UTC

    And the answer from RosettaCode:

    sub zip (&@) { my $code = shift; my $min; $min = $min && $#$_ > $min ? $min : $#$_ for @_; for my $i ( 0 .. $min ) { $code->( map $_->[$i] , @_ ); } } my @a1 = qw( a b c ); my @a2 = qw( A B C ); my @a3 = qw( 1 2 3 ); zip { print @_,"\n" } \( @a1, @a2, @a3 );

    This implementation will stop producing items when the shortest array ends.

Re: How do I Loop over multiple arrays simultaneously ?
by jdporter (Paladin) on Aug 07, 2012 at 15:44 UTC
    use List::MoreUtils::each_array:
    use List::MoreUtils qw( each_array ); my @a = qw( a b c ); my @b = qw( A B C ); my @c = qw( 1 2 3 ); my $ea = each_array( @a, @b, @c ); while ( my( $a, $b, $c ) = $ea->() ) { print $a, $b, $c; }
    :-)
Re: How do I Loop over multiple arrays simultaneously?
by gaurav.trikha (Initiate) on Aug 07, 2012 at 14:02 UTC

    Well, assuming that it's certain that the arrays are all the same length, just translate the statement of the problem into Perl: have an index going the length of one of them, and then use it in all of them:

    my @a = qw( a b c ); my @b = qw( A B C ); my @c = qw( 1 2 3 ); for my $i ( 0 .. $#a ) { print $a[$i], $b[$i], $c[$i]; }
Re: How do I Loop over multiple arrays simultaneously ?
by simmisam (Novice) on Jan 15, 2014 at 06:32 UTC

    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);
      Due to the string concatenation, this doesn't work if my arrays have nested data structures.
Re: How do I Loop over multiple arrays simultaneously ?
by bhumip (Initiate) on Dec 17, 2010 at 09:30 UTC
    my @a1 = qw( a b c d); my @a2 = qw( A B C D); my @a3 = qw( 1 2 3 4); my @a4 = qw( 11 22 33 44); zip(\@a1,\@a2,\@a3,\@a4); sub zip { for ( my $i = 0; $i <= $#{$_[0]}; $i++ ) { print map( @{$_}[$i], @_ ), "\n" ; } }
      Or use the zip function in List::MoreUtils, which is a common enough module to be installed almost everywhere anyway as a prerequisite to something else.
Re: How do I Loop over multiple arrays simultaneously ?
by simmisam (Novice) on Feb 03, 2014 at 00:46 UTC

    Try this

    my $index = $ARGV[0]; chomp($index); my @a1 = qw("a" 'b' 'c' 'd'); my @a2 = qw('A' 'B' 'C' 'D' 'E'); my @a3 = qw(1 2 3 4 5 6 7 8 9); multipleArray(\@a1,\@a2,\@a3); sub multipleArray { while (@_) { my $array = shift; print "${$array}[$index]\n"; } }

    Originally posted as a Categorized Answer.

A reply falls below the community's threshold of quality. You may see it by logging in.