I am having one program, which read a line by line from a file, if the line matches for the particular pattern, then i should read nth line from a current line.
some simple example is:
open(FH,"test.txt"); while ( <FH> ) { ## If the line matches for this pattern, I should read 4th lin +e from current line if ( $_ =~/you have to read the fourth line/ ){ $i=0; while ($i<4) { $line = <FH>; $i++; } print $line; } ## If the line matches for this pattern, I should read 2nd lin +e from current line elsif ( $_ =~/you have to read the second line/ ){ $i=0; while ($i<2) { $line = <FH>; $i++; } print $line; } }
Input:
first line
second line
third line
you have to read the fourth line
one
two
three
four
five
six
you have to read the second line
one
two
three
output:
four
two
The above coding works for me for my requirement
but i felt that the way that i have handled to read nth line from a file, is inefficient.. kindly advice some efficient way to do the above.
In reply to Reading nth line from a file by perlthirst
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |