Next Tricks of the Wizards 208

combine

        @list1 = (1,2,3,4,5);
        @list2 = (2,3,5,7,11);
        @result = combine { $a + $b } @list1, @list2;

@result is (3,5,8,11,16)

        sub combine (&\@\@) {
          my ($code, $ar1, $ar2) = @_;
          my @result;
          while (@$ar1 && @$ar2) {
            local $a = shift @$ar1;
            local $b = shift @$ar2;
            push @result, &$code;   
          }
          @result;
        }


Next Copyright © 2003 M. J. Dominus