in reply to Re^2: how to read multiple lines from a file
in thread how to read multiple line from a file
Hello skyworld_chen,
The solutions by 2teez and TJPride below are good ones, but if you want to adapt my Tie::File approach, just use a sliding window:
#! perl use strict; use warnings; use Tie::File; my $filename = 'data.txt'; tie my @lines, 'Tie::File', $filename or die "Cannot tie file '$filena +me': $!"; my @search_words = ('jumped', 'over'); for (0 .. $#lines - 1) { if ($lines[$_ ] =~ /$search_words[0]/ && $lines[$_ + 1] =~ /$search_words[1]/) { print "\nFound '$search_words[0]' on line ", ($_ + 1), ", and '$search_words[1]' on line ", ($_ + 2), "\n"; last; } } untie @lines;
Output:
16:02 >perl 605_SoPW.pl Found 'jumped' on line 5, and 'over' on line 6 16:03 >perl 605_SoPW.pl
Your precise requirements are not clear (to me), but this technique can be easily adapted as needed.
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|