Re: how to read multiple line from a file
by TJPride (Pilgrim) on Apr 13, 2013 at 06:16 UTC
|
If you really need a line-based approach rather than just loading the whole thing into memory (most of the time, the latter approach will work just fine), then you need to cache the previous lines and do a search of the cache:
use strict;
use warnings;
my $csize = 2; ### Number of lines to save
my (@cache, $str, $c);
my $file = 'data.txt';
open (IN, $file) or die "Can't open $file for read.";
while (<IN>) {
shift @cache if $#cache == $csize - 1;
push @cache, $_;
$c++;
$str = join '', @cache;
if ($str =~ /jumped\nover/) {
print "String found in $file at lines " . ($c - $#cache) . "-$
+c:\n";
print $str;
last;
}
}
Input file data.txt:
the
quick
brown
fox
jumped
over
the
unfortunate
dog
Output:
String found in data.txt at lines 5-6:
jumped
over
| [reply] [d/l] [select] |
|
Just a minor note from the C programmer in me: the easiest and most efficient way to do a fixed-size queue is to use the mod operator:
while (<>) {
$buf[$. % $size] = $_;
}
| [reply] [d/l] |
|
As a minor note of a functional programmer, I'll put the logic in an extra iterator like here:
Re: Split string after 14 Line Feeds?.
This makes the code much more readable. =)
Otherwise I'm not sure where you want the processing logic to happen in your code, maybe something like unless ($. % $size) { ...} within the loop?
I think If a sliding window is needed, shift and push might be better.
Cheers Rolf
( addicted to the Perl Programming Language)
| [reply] [d/l] [select] |
|
|
|
|
| [reply] |
|
|
thanks for all of your replies. I will check these ideas again.
| [reply] |
Re: how to read multiple line from a file
by 2teez (Vicar) on Apr 13, 2013 at 05:46 UTC
|
use warnings;
use strict;
use feature 'say';
my $str = do {
local $/;
<DATA>;
};
say join " " => grep { $_ if /fox/ .. /over/ } split /\n/, $str;
__DATA__
the
quick
brown
fox
jumped
over
the
unfortunate
dog
Output
fox jumped over
NOTE:
I used Athanasius data since the OP has none.
If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me
| [reply] [d/l] [select] |
Re: how to read multiple lines from a file
by Athanasius (Archbishop) on Apr 13, 2013 at 03:09 UTC
|
One approach is to use the core module Tie::File. For example, if you have an input file “data.txt” containing:
the
quick
brown
fox
jumped
over
the
unfortunate
dog
Then this script:
#! 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': $!";
chomp(my @text = @lines[3 .. 5]);
print "\n", join(' ', @text), "\n";
untie @lines;
will produce this output:
13:01 >perl 605_SoPW.pl
fox jumped over
13:06 >
Hope that helps,
| [reply] [d/l] [select] |
|
thanks Athanasus. The way you provide is good, but if I want to search a special word across several lines within a file, for example, if a line contains a word "one" and the line next to it contains a word "two", this "tie" seems not as good as I expect. anyway, thanks for your idea
| [reply] |
|
#! 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,
| [reply] [d/l] [select] |
Re: how to read multiple line from a file
by roho (Bishop) on Apr 13, 2013 at 12:06 UTC
|
Take a look at 'natatime' in List::MoreUtils.
"Its not how hard you work, its how much you get done."
| [reply] |