[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Iternating over multiple lists



At 06:20 PM 5/18/00 -0500, David L. Nicol wrote:
>sub map4(&\@\@\@\@){
>
>         local $[ = 0;
>
>         my ($a,$b,$c,$d);
>         my $i = 0;
>         my $block = shift;
>         my @r, @result = ();
>
>         TOP:
>         ($a,$b,$c,$d) = ($$_[0][$i],$$_[1][$i],$$_[2][$i],$$_[3][$i++]);
>
>         # adjust next line to suit
>         defined $a or $defined $b or $defined $c or $defined $d or return 
> @result;
>
>         {
>         my @_ = ($a,$b,$c,$d);
>         @r=(&$block);
>         }
>
>         push @result, @r
>
>         goto TOP;
>};
>
>Is that correct?  Is that what the discussion has gotten to so far?

Well, we can improve on it a bit... what if array entries are undef, what 
if the code block wants to modify an array entry in place like map can, and 
ya gotta lose that goto :-)  Here's a map2, generalize to suit:

sub nmap(&\@\@) {
   my $block = shift;
   my @results;
   for (my $i = 0; @{$_[0]} > $i || @{$_[1]} > $i; $i++) {
     push @results, $block->($_[0][$i], $_[1][$i]);
   }
   @results;
}


>Do we have variable-length prototypes,
>sub mmap(&\@+)
>yet?

No, wouldn't that be cool...

--
Peter Scott
Pacific Systems Design Technologies


==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe