From: Sven Schöling Date: Tue, 10 Mar 2009 17:09:21 +0000 (+0000) Subject: Kreuzprodukt aus zwei Arrays in List::MoreUtils Stil. X-Git-Tag: release-2.6.0beta2~149 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=0975a120536e57a0f563e04b33f33f14ce8ba89e;p=kivitendo-erp.git Kreuzprodukt aus zwei Arrays in List::MoreUtils Stil. usage: @crossed_array = cross { func($a, $b) } @array1, @array2; --- diff --git a/bin/mozilla/common.pl b/bin/mozilla/common.pl index 1c308dac7..16e1b4675 100644 --- a/bin/mozilla/common.pl +++ b/bin/mozilla/common.pl @@ -580,4 +580,31 @@ sub cov_selection_internal { $lxdebug->leave_sub(); } +sub cross(&\@\@) { + my $op = shift; + use vars qw/@A @B/; + local (*A, *B) = @_; # syms for caller's input arrays + + # Localise $a, $b + my ($caller_a, $caller_b) = do + { + my $pkg = caller(); + no strict 'refs'; + \*{$pkg.'::a'}, \*{$pkg.'::b'}; + }; + + my $limit = $#A > $#B? $#A : $#B; # loop iteration limit + + local(*$caller_a, *$caller_b); + + # This map expression is also the return value. + map { my $b_index = $_; + map { my $a_index = $_; + # assign to $a, $b as refs to caller's array elements + (*$caller_a, *$caller_b) = \($A[$a_index], $B[$b_index]); + $op->(); # perform the transformation + } 0 .. $#A; + } 0 .. $#B; +} + 1;