in reply to reg ex matching 101

Broken: while ($line = <READTHIS>) { if ($line =~ /cat/) { print "yeppers!"; } }</I>
change that to be:
while(<READTHIS>){ if($_ =~ /cat/) { print "Yeppers!\n" } else{ print $_; }
Keep in mind, you are going to get any match on cat like "catching" a cold...I dunno if you wanted that or not. If you don't mind me asking, what was your intent for the script? I was trying to figure out what you were up to with the search and replace lines. In some of those you are searching for a set of characters and replacing them with the identical data.
btw, go home and get some pizza:P

Replies are listed 'Best First'.
RE: RE: reg ex matching 101
by Petruchio (Vicar) on Oct 07, 2000 at 08:36 UTC
    Good call on the loop, but it's still not gonna work unless you knock out print <READTHIS>; I haven't looked at it closely, but the stuff up top seems to be homemade CGI; and there's a hard-to-see space in s/~!/ ~!/g
RE: RE: reg ex matching 101
by chromatic (Archbishop) on Oct 07, 2000 at 20:22 UTC
    What's wrong with the original?

    It doesn't handle word boundaries, as you mention, but I don't see much of a difference between the magical assignment to $_ and the explicit assignment to $line.

    Besides that, you could just use print "Yeppers!" if /cat/; and print;.

    If I'm missing something here, please enlighten me.

      the use of $_ was a 'code to taste' manuever on my part. I use the magical assignments all the time:)
      What I saw was the file being printed out, then the while loop trying to print 'yeppers' each time cat was found.
      While I was unaware that printing the file stream and then trying to call it in the while would fail, I imagined the output to be the full file printing, and then any number of 'Yeppers' one after the other. I was assuming what he wanted to do was print yeppers along with the text instead of one after the other.