Re: Why '' Is Matched First Time, Not Second Time
From: Eric Bohlman (ebohlman_at_omsdev.com)
Date: 08/21/04
- Next message: Brad Baxter: "Re: Can I modify scalars contained in the array ref returned by DBI"
- Previous message: Gunnar Hjalmarsson: "Re: Why '' Is Matched First Time, Not Second Time"
- In reply to: http://links.i6networks.com: "Why '' Is Matched First Time, Not Second Time"
- Next in thread: http://links.i6networks.com: "Re: Why '' Is Matched First Time, Not Second Time"
- Reply: http://links.i6networks.com: "Re: Why '' Is Matched First Time, Not Second Time"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 21 Aug 2004 01:23:08 GMT
"http://links.i6networks.com" <SaveWorldFromAids@alexa.com> wrote in
news:YLwVc.1833114$Ar.1019409@twister01.bloor.is.net.cable.rogers.com:
> ##################Perl File
> my @line=(); my $foundcat=0;
> my $keyword1=''; my $keywordcat='';
> open ($INPUT, 'ostkall.txt') || die "can't open $file: $!";
> while (<$INPUT>) {
> chomp;
> if (!($_=~ m/.+\|.+\|.+/)) {
> if
> ($_=~ m/$keywordcat/i)
> {
> $foundcat=1;
> print "FOUND $_\n";
>
> } else {$foundcat=0;}
> } #end of if (!($_=~ m/.+\|.+\|.+/)
>
> }
> close($INPUT) ;
>
> #####################The output is
> FOUND Computers & Printers|Monitors
>
> But it should be
> FOUND Computers & Printers|Monitors
> FOUND Computers & Printers|Priners
>
>
> #################the data in ostkall.txt is
> Computers & Printers|Monitors
> | | | | |
> Computers & Printers|Printers
> | | | | |
>
> ############# Question
> Empty String '' is matched the first time but not the second time. Why?
I must admit I played around with your code for about 15 minutes, got
rather puzzled, and then got this nagging feeling "there must be something
special about null-string matches." So I did what every good Perl
programmer does when they get such a feeling and read the section on the
match operator in perlop. Sure enough, there it was:
"If the PATTERN evaluates to the empty string, the last successfully
matched regular expression is used instead. In this case, only the g and c
flags on the empty pattern is honoured - the other flags are taken from the
original pattern. If no match has previously succeeded, this will
(silently) act instead as a genuine empty pattern (which will always
match)."
When you read the very first line, no regexp has yet successfully matched,
so /$keywordcat/ matches the empty string. When you read the next line,
/.+\|.+\|.+/ successfully matches, so when you go on to read the third
line, /$keywordcat/ is now being treated as /.+\|.+\|.+/, which of course
doesn't match there.
BTW, another thing good Perl programmers do is put 'use warnings;' and 'use
strict' at the top of their programs. If you did that, you'd see that your
error message for the open was using a variable that gallopped in from
nowhere.
- Next message: Brad Baxter: "Re: Can I modify scalars contained in the array ref returned by DBI"
- Previous message: Gunnar Hjalmarsson: "Re: Why '' Is Matched First Time, Not Second Time"
- In reply to: http://links.i6networks.com: "Why '' Is Matched First Time, Not Second Time"
- Next in thread: http://links.i6networks.com: "Re: Why '' Is Matched First Time, Not Second Time"
- Reply: http://links.i6networks.com: "Re: Why '' Is Matched First Time, Not Second Time"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|