Re: Why '' Is Matched First Time, Not Second Time

From: Eric Bohlman (ebohlman_at_omsdev.com)
Date: 08/21/04


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.



Relevant Pages

  • Re: Bad protocol tcp
    ... empty string. ... This is also true for 'getprotobynumber'. ... on the other computers returns the appropriate values as indicated in ... the protocol file. ...
    (comp.lang.perl.misc)
  • Re: How Can I get the Processor Id from my computer
    ... Change the "SELECT SerialNumber" ... "Juan Manuel Alegría B." ... > computers, but in some computers doesn't return the serial number I mean ... > returns as blank space or empty string. ...
    (microsoft.public.dotnet.general)
  • Why Is Matched First Time, Not Second Time
    ... FOUND Computers & Printers|Monitors ... Empty String '' is matched the first time but not the second time. ...
    (comp.lang.perl.misc)