Dear Perlers,
I am trying to print the matching lines using __DATA__ filehandle and for the
very first time it prints the desired lines, but as soon as I rewind it using
seek, it is printing lines from the very beginning which is not the desired
result (as it prints from the start). I want to rewind only the __DATA__ part.
Please help to print the matching lines using approach 2 and 3 as well.
[code]
use strict;
use warnings;
#Approach 1
#Printing lines 2 through 5 using range operator
while (<DATA>) {
print if 2 .. 5;
}
print "Info: The position of DATA file handle is ", tell DATA, "\n";
seek DATA, 0, 0;
print "Info: The position of DATA file handle is ", tell DATA, "\n";
#Approach 2
#Printing lines 2 through 5 using counter.
my $counter = 1;
while (<DATA>) {
print if $counter >= 2 && $counter <= 5;
$counter++;
}
print "Info: The position of DATA file handle is ", tell DATA, "\n";
seek DATA, 0, 0;
print "Info: The position of DATA file handle is ", tell DATA, "\n";
#Printing lines 2 through 5 using $.
while (<DATA>) {
print if $. >= 2 && $. <= 5;
}
__DATA__
India
Srilanka
USA
Nepal
France
UK
Australia
New Zealand
[/code]
[output]
Srilanka
USA
Nepal
France
Info: The position of DATA file handle is 777
Info: The position of DATA file handle is 0
use warnings;
#Approach 1
#Printing lines 2 through 5 using range operator
Info: The position of DATA file handle is 777
Info: The position of DATA file handle is 0
[/output]
Thank you.
best,
Shaji
-------------------------------------------------------------------------------
Your talent is God's gift to you. What you do with it is your gift back to God.
-------------------------------------------------------------------------------