[boulder.pm] next in a for loop?

Pete Krawczyk petek at bsod.net
Mon Jun 18 11:09:28 CDT 2001


Date: Mon, 18 Jun 2001 09:49:48 -0600
From: Robert L. Harris <Robert.L.Harris at rdlg.net>
Subject: [boulder.pm] next in a for loop?

}  foreach $i (@content) {
}    next if ($i =~ /^$/);
   <assuming processing stuff>
}    print "\$i :$i:\n";
}  }

}How do I get it to go to the next iteration of the loop if $i is empty?

The problem being that $i probably isn't empty.

Remember, if $i contains "\n", that's sufficient to make it not do the
next.

So, first and foremost, I'd throw in a "chomp($i);" to the top of the
loop.

Next, in your debugging, what's between the colons?  If it's nothing
(i.e. it says "$i ::"), then there may be a problem between the next and
print which is incorrectly editing $i.  Try putting that print immediately
after the next.

And if you really, really want to make absolutely sure that the next goes
with the foreach, you can always do:

UGLYLOOP: foreach my $i (@content) {
  next UGLYLOOP if $i =~ /^$/;
  ...
}

(As an aside, that's why if I don't need blank lines, I generally write
that statement as "next if $i =~ /^\s*$/;".)

-Pete K
-- 
Pete Krawczyk
  petek at bsod dot net
  http://www.bsod.net/~petek/





More information about the Boulder-pm mailing list