Here's another "What's a quick and real neat way of...." type question

Ewen McNeill ewen at
Thu Nov 14 16:25:09 CST 2002

In message <5FA042F680739D44951B00FED8BE1A7D06A091 at>, "S
haun McCarthy" writes:
>In which case I think:
>while (@a && my($a, $b) = (shift @a, shift @b)) {

my $len = ((scalar @a) <= (scalar @a) ? (scalar @a) : (scalar @b));
for (my $i = 0; $i < $len; ++$i)
  my ($a, $b) = (@a[$i], @b[$i]);

or if you're willing to destroy @a and @b in the process (it seems
everyone is):

while ((scalar @a) > 0 && (scalar @b) > 0)
  my ($a, $b) = (shift @a, shift @b);

(This second one will be expensive if scalar(@array) is O(n); ISTR that perl 
uses an array structure that means that scalar(@array) is O(1).)

Just because you can somehow cram everything onto one line doesn't mean
that you _have_ to do so :-)  But if you feel you really must...

while ((scalar @a) > 0 && (scalar @b) > 0 && (($a, $b) = (shift @a,shift @b)))
  # body goes here

And so I don't get left out of the "bizzare ways to do this"

@c = map { print "$a[$_], $b[$_]\n" } (0..((scalar @a)-1));


More information about the Wellington-pm mailing list