in reply to Perl script to print Next line after Pattern Matching
Then, the logic of your script fails because your spec says to capture the contents of two lines, separated by a newline.
The direct way of dealing with that, if your representation of the data is correct, is probably to read the data in paragraph mode (perldoc -q paragraph, 2nd item, citing perlfaq5) and search using a regex that allows an embedded newline (any decent regex tut).Or, without setting the read mode to paragraph, you could read a line, cache it (as say $line) and immediately read another line into $line2; then test line 1 for the "Yes" and if there's a match, print both to your out.file, clear both vars and repeat while your source file has more data in it. Effectively, this is merely a simple-minded (and more verbose) variant on the first suggestion.
Alternately -- but in the same vein -- you could cache each line where "ID: Yes" is true; read the next line; and print that to your out.file. Be warned, the logic of doing that is not simple: Among other things, you'll need to get the program flow back to the main data input after saving a match.
Also, there's no reason for allowing potential confusion by using both $line and $_as you do: if my $line = <$in_fh> then use the named var, thereafter.
And I'm baffled as to how, where, or why you've come up with line 10; this may merely be another ww blind-spot, but I suspect it's a case of where you tried to make something up and hoped the computer would understand. As a general rule, that won't work.
Oddly (to me, anyway) your existing code passes a compile test under 5.12 So long as the pragmas strict and warnings are NOT in the code. However, when you use those to have perl help you correct your mis-steps; it's quite a different story. Use strict and warnings until you're so sure of yourself you can be positive you need to NOT use them.
And re your "Optional" question -- All answers here are optional Whether or not the Monks exercise the option of answering depends, in part, on how carefully you've written up your dilemma. When you're inclined to write (as you did) "i have written a script which is throwing up errors, we expect you to post the error(s and warnings), verbaitim, inside <code>...</code tags... and perhaps, if any, even a sample of any other output you are getting.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perl script to print Next line after Pattern Matching
by Anonymous Monk on Dec 21, 2011 at 04:08 UTC |