in reply to reg ex matching 101
Confess! You just stuck use strict in there to make us happy, didn't you? This script won't run at all, as is. Besides the fact that none of the variables are scoped using my or local, you forgot to put the semicolon after strict. Heheh... nice try! :-)
Also, one must wonder why you don't use CGI. Put in the slight effort to learn it... you'll be glad you did.
Now... the problem is with
print (<READTHIS>);
A filehandle, used in list context, will return all the lines it has left. Since print likes lists, the whole file gets printed out verbatim right away. Then, when you get to
while ($line = <READTHIS>) { if ($line =~ /cat/) { print "yeppers!"; } }
there's nothing left.
Anyway, your while loop doesn't do what I think you think it does. The fact that you thought the regex was the problem leads me to think you actually wanted to say,
while ($line = <READTHIS>) { if ($line =~ /cat/) { print "yeppers!"; } else{ print $line; } }
and leave out,
print (<READTHIS>);
If you want to find out where your problem is, by the way, you can often do it by sticking print statements in odd places. For instance, you could have stuck one in here:
while ($line = <READTHIS>) { print "MOO!\n"; if ($line =~ /cat/) { print "yeppers!"; } }
Then, since you didn't hear a herd of cows coming through, you'd have realized that you actually weren't going through the loop at all.
P.S. Throw close READTHIS; down near the bottom, just for fun!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: Re: reg ex matching 101
by NotProud (Novice) on Oct 09, 2000 at 05:35 UTC | |
|
RE: Re: reg ex matching 101
by NotProud (Novice) on Oct 09, 2000 at 05:39 UTC |