Forum Moderators: coopster & phranque

Message Too Old, No Replies

omit certain lines

         

SlowMove

4:10 pm on Feb 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I understand that I can use the following code to print lines that are not equal to "foo"


while ($line ne "foo") {
print $line;
}

Does anyone know how not to print the lines that have "foo" as a substring?

tschild

5:42 pm on Feb 3, 2004 (gmt 0)

10+ Year Member



You'd replace the condition with

$line!~ m/foo/

Your code will enter an infinite loop when the condition is true, though. Better alternatives:

(assuming $line has been assigned somewhere else)


unless ($line =~ m/foo/) {
print $line;
}

or

(assuming lines are read in from stdin)


while ($line = <>) {
unless ($line =~ m/foo/) {
print $line;
}
}

(sorry, board ate spaces somehow)