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

Worik worik at noggon.co.nz
Thu Nov 14 15:47:06 CST 2002


On Thu, 2002-11-14 at 23:46, Piers Harding wrote:
> 
> how about this:
> 
> while (my ($a, $b) = (shift @a, shift @b)){
> 
>   do the funky thing with $a and $b ....
> 
> }
> 
> Cheers.

The problen is that the loop never finishes.  I implemented this and
tested it (I was supprised it was an infinite loop).

Check this out...
if((undef, undef)){
    print "Dual undef is true\n";
}
if(my($a, $b) = (undef, undef)){
    print "Dual undef assignment is true\n";
}

The first one fails the second one succeeds....

My solution is to not get too cute.  

#!/usr/bin/perl -w
use strict;
my @a = qw(a b c d e f g);
my @b = qw(1 2 3 4 5 6 7 8);
my $max = $#a < $#b ? $#a : $#b; 
for my $i (0..$max){
    print "\$a $a[$i]\t\$b\t$b[$i]\n";
}

Worik	
	

> 
> 
> On Thu, Nov 14, 2002 at 11:06:14PM +1300, Enkidu wrote:
> > Say you have two arrays @a and @b.
> > 
> > You can loop through one array by using foreach:
> > 
> > for my $file (@a) {
> > 	<do something with $file> ; 
> > }
> >  
> > Is there a neat way of looping through *both arrays* at the same time?
> > Something like:
> > 
> > for my ($file, $desc) (@a, @b) {
> > 	<do something with $file, $desc> ;
> > } 
> > 
> > Cheers,
> > 
> > Cliff
> > --  
> > 
> > The Nats held a Party and no one came.
> 




More information about the Wellington-pm mailing list