in reply to Foreach two arrays?

How about using the same index variable to loop through each array:
#!/usr/bin/perl -wT use strict; my @a = qw(1 2 3 4 5 6); my @b = qw(a b c d e f g h i); my $max = $#a > $#b ? $#a : $#b; # find the max index of the biggest +array for my $i (0..$max) { # loop through using the same index +variable print "$a[$i]\n" if defined $a[$i]; print "$b[$i]\n" if defined $b[$i]; } =OUTPUT 1 a 2 b 3 c 4 d 5 e 6 f g h i

-Blake

Replies are listed 'Best First'.
Re: Re: Foreach two arrays?
by broquaint (Abbot) on Sep 06, 2001 at 17:15 UTC
    Or you could controversially use the 'for' loop as used in pre-perlhistoric times -
    @a = qw(look the for); @b = qw(at lovely loop); for($i = 0; $i < @a; $i++) { print "$a[$i] $b[$i] "; }
    resulting in -
    look at the lovely for loop

    Just my $0.02 (not that I'm one to arbitrarily dispense money ;o)

    broquaint

Re: Re: Foreach two arrays?
by Dylan (Monk) on Sep 06, 2001 at 10:12 UTC
    Ohh Ahh :)

    I like that.