Re: [OT] Perl pattern matching
On Fri, Dec 06, 2002 at 04:53:28PM +0100, J?rg Johannes wrote:
> Here it comes:
> In "Programming Perl (german translation) there is an example for how to
> skip lines that start with the hash sign:
>
> LINE: while ($line = <INFILE>) {
> next LINE if /^#/; # Should skip lines that start with the hash
> sign.
> # what to do with non-comment-lines....
> }
Instead of the second line there, you need:
next LINE if $line =~ /^#/;
... otherwise you're matching against $_, which you haven't set.
Actually that example is wrong in other ways: it'll break if you read a
line containing a false string value like "0", I think. Use one of these
instead:
while (<INFILE>) {
next if /^#/;
# the input line is in $_
}
while (defined($line = <INFILE>)) {
next if $line =~ /^#/;
# the input line is in $line
}
> PS.: The book is written for Perl 5.006, I think, and I use 5.8 (from
> sid). Did the pattern matching change?
Nope, the example you quote is wrong in both.
Cheers,
--
Colin Watson [cjwatson@flatline.org.uk]
Reply to: