For multiple arrays

In a schema, you can iterate over multiple lists with for-each :

 > (for-each (lambda (ab) (display (+ ab)) (newline)) '(10 20 30) '(1 2 3)) 11 22 33 > 

I know that in Perl you can use for to iterate over a single list. What is a good way to iterate over multiple lists, as an example schema?

I'm interested in answers for Perl 5 or 6.

+8
perl perl6
source share
8 answers

In Perl 5, you can use the List :: MoreUtils module . Either with a pair, or with an iterator returned by each_array (which can take more than two arrays for parallel iteration).

 use 5.12.0; use List::MoreUtils qw(pairwise each_array); my @one = qw(abcde); my @two = qw(qwert); my @three = pairwise {"$a:$b"} @one, @two; say join(" ", @three); my $it = each_array(@one, @two); while (my @elems = $it->()) { say "$elems[0] and $elems[1]"; } 
+9
source share

In Perl 6, the Zip operator is most preferred. If you want to get both values ​​(and not calculate the amount directly), you can use it unsigned plus:

 for (10, 11, 12) Z (1, 2, 3) -> $a, $b { say "$a and $b"; } 
+10
source share

With the Zip operator, you can achieve what you do with the scheme:

 > .say for (10, 20, 30) Z+ (1, 2, 3) 11 22 33 

See http://perlcabal.org/syn/S03.html#Zip_operators

+6
source share

You can simply iterate over the indexes of arrays if you are sure that they are the same size:

 foreach( 0 .. $#array1 ) { print $array1[$_] + $array2[$_], "\n"; } 
+2
source share

Algorithm :: Loops offers the MapCar function to iterate over multiple arrays (with options that have different relationships with arrays of uneven size).

+2
source share

One of the methods:

 sub for_each { my $proc = shift ; my $len = @{$_[0]} ; for ( my $i = 0 ; $i < $len ; $i++ ) { my @args = map $_->[$i] , @_ ; &$proc ( @args ) ; } } for_each sub { say $_[0] + $_[1] } , ([10,20,30],[1,2,3]) 

Using each_arrayref from List::MoreUtils :

 sub for_each { my $proc = shift ; my $it = each_arrayref ( @_ ) ; while ( my @elts = $it->() ) { &$proc ( @elts ) } ; } for_each sub { say $_[0] + $_[1] } , ([10,20,30],[1,2,3]) 

Thanks to Alex for listing List::MoreUtils .

+1
source share

While the question is as simple as adding (/ multiplying, division, ...) and Arrays are not arrays of arrays, you can also use hyperoperators for your task:

 <1 2 3> Β«+Β» <10 20 30> 

(of course, this is Perl6 answer)

if you don’t have access to French quotes «» , you can rewrite it as

 <1 2 3> <<+>> <10 20 30> 
+1
source share

One solution (there are many to choose from) may look like this:

 my @argle = (10, 20, 30); my @bargle = (1, 2, 3); do { my $yin = shift @argle; my $yang = shift @bargle; say $yin + $yang; } while (@argle && @bargle); 

I feel like you are asking about foreach , which might look like this:

 my @argle = (10, 20, 30); my @bargle = (1, 2, 3); for my $yin (@argle) { my $yang = shift @bargle; say $yin + $yang; } 

But in this case it is not so smooth. What happens if any array is shorter?

0
source share

All Articles