in reply to read 2 array simultaneously

And in the spirit of TIMTOWTDI, the Perl6 inspired ...

sub zip { my ($ref1, $ref2) = @_; my @zip; while (@$ref1 || @$ref2) { push @zip => shift @$ref1 || undef; push @zip => shift @$ref2 || undef; } return @zip; } my $x = 1; my $y = 2; my @a=qw(1 2 3); my @b=qw(7 8 9); my @zip = zip(\@a, \@b); while (my ($item, $element) = splice @zip => 0, 2) { print "$x: $item $y: $element\n"; }

Better written as:

for zip(@a,@b) -> $item, $element { say "$x: $item $y: $element"; }

But you'll have to wait a bit for that syntax to work :)

Cheers,
Ovid

New address of my CGI Course.

Replies are listed 'Best First'.
Re^2: read 2 array simultaneously
by thor (Priest) on Jan 12, 2005 at 04:13 UTC
    Just for my own edification, do you need to do "shift @$ref1 || undef"? shift will return undef if there are no elements in the array.

    thor

    Feel the white light, the light within
    Be your own disciple, fan the sparks of will
    For all of us waiting, your kingdom will come

      No, it's not necessary. I was just being silly when I threw that together. I didn't give it too much thought.

      Cheers,
      Ovid

      New address of my CGI Course.