in reply to Re: parallel procesing
in thread parallel procesing

Personally I prefer to use a non-iterator style, so I combine (my own) zip with pair from List::Pairwise.

for (pair(zip($array1, $array2))) { my ($x, $y) = @$_; # It's often descriptive to name the values. ... }
The main reason I like pair over natatime is that it's easy to combine with any expression/subroutine that requires the whole list and in particular sort (but also map and grep). For instance:
for ( sort { $a->[0] <=> $b->[0] or $a->[1] cmp $b->[1] } pair zip($array1, $array2) ) { my ($number, $char) = @$_; ... }
The reason I don't use zip from List::MoreUtils is that it's prototyped with (\@\@;\@...) which I find counter-intuitive, as I imagine you do too as you passed in $array1 instead of @$array1. It's also more in the way than it's helping, as I frequently end up with wanting to zip the result of an expression instead of an array or having a list or an array holding array references.

lodin

Replies are listed 'Best First'.
Re^3: parallel procesing (List::Pairwise)
by baxy77bax (Deacon) on Apr 06, 2008 at 14:12 UTC
    i have a problem with zip subroutine. I've installed mentioned modules but the zip subroutine could not be executed because it isn't there. you said "I combine (my own) zip " where is the publicly available zip situated ???

      Assuming the same number of elements in each array you could:

      push @$zip, map {shift @$_} ($array1, $array2) while @$array1;

      If you instead wanted an array of paired rather than zipped elements you could:

      push @$array, [map {shift @$_} ($array1, $array2)] while @$array1;

      Note however that both of these destroy the original arrays.


      Perl is environmentally friendly - it saves trees

        Non-destructive:

        my @array = map { [ $array1->[$_], $array2->[$_] ] } 0..$#$array1;

        or

        my @array; push @array, [ $array1->[$_], $array2->[$_] ] for 0..$#$array1;