baxy77bax has asked for the wisdom of the Perl Monks concerning the following question:

hi, the thing is i need to do two things in a parallel. for example i have one @array1 with strings in it and the second one also with some data in it @array2. now i wish to go through data in one @array1, return the string from it and connect it with a string of the same index in the second @array2. Example:
$array1 = [qw(1 2 3 4 5 6 7 8 9 0)]; $array2 = [qw(a s d f g h j k l o)];
meaning i wish to retrive 1 from the @array1 than a from the @array2, print the result and than redo the process so that the end result looks something like this:
1,a 2,s 3,d 4,f 5,g 6,h 7,j 8,k 9,l 0,o
is there a solution with a foreach() function, something like:
froeach my $x (@$array1) { return $x; } foreach my $c (@$array2) { return $c; } print qq($x , $c);
obviously this doesn't work, but if a could some how parallelize the prosea so that it returns one character from the first arrya than the first from the second one than it prints it and repeats the proces for the second characters in the arrays. thank you , Robert

Replies are listed 'Best First'.
Re: parallel procesing
by ikegami (Patriarch) on Apr 06, 2008 at 09:51 UTC
    Iterate over the indexes:
    for my $i (0..$#$array1) { print("$array1->[$i], $array2->[$i]\n"); }
    or
    for my $i (0..$#$array1) { my ($x, $c) = ($array1->[$i], $array2->[$i]); print("$x, $c\n"); }
Re: parallel procesing
by Corion (Patriarch) on Apr 06, 2008 at 09:51 UTC

    I think you want a combination of zip and natatime ("n-at-a-time") from List::MoreUtils:

    my $pairs = natatime 2, zip $array1, $array2; while (my (@vals) = $pairs->()) { ... };

    zip "zips" together two lists, alternating the elements. natatime returns you n elements from a list.

      If you're going to use List::MoreUtils for something this trivial, then each_arrayref does everything in one step.
      my $i = each_arrayref($array1, $array2); while (my ($x, $c) = $i->()) { print("$x, $c\n"); }

      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

        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 ???
Re: parallel procesing
by GrandFather (Saint) on Apr 06, 2008 at 20:58 UTC

    If you have the option you may be better to combine the two arrays into on array of pairs of elements.


    Perl is environmentally friendly - it saves trees
Re: parallel procesing
by poolpi (Hermit) on Apr 07, 2008 at 08:30 UTC

    is there a solution with a foreach() function

    print +(@$array1,@$array2)[$_, $#$array1+1+$_], "\n" for 0..$#$array1;

    hth,
    PooLpi

    'Ebry haffa hoe hab im tik a bush'. Jamaican proverb