in reply to regular expression

The idea is to insert 'hello' after the marker line. For each line, it either prints the line plus hello (if it's the marker line) or just the line. The loop body could be written better as

print FILE $line_in; if ($line_in =~ /The marker line/) { print FILE "hello\n"; }

Replies are listed 'Best First'.
Re^2: regular expression
by CountZero (Bishop) on Oct 31, 2010 at 17:37 UTC
    For each line, it either prints the line plus hello (if it's the marker line) ...
    That is not entirely true.

    The test checks if the line contains the literal text "The marker line", but there could be other text before or after it. However, it wil then only print "The marker line" into the file, actually discarding some "garbage" text.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      I said "The idea is". I realise it doesn't quite succeed at executing it.