Dear Paul,

Many thanks for your explanation on Approach 3

Here is the working code

Courtesy : 

1) Jim Gibson (For Approach 1 and 2)
2) Paul Johnson (For Approach 3)
3) John W Krahn (I figured it out myself, thanks for emphasizing the fact to 
greater audience)

[code]
use strict;
use warnings;

my $data_position = tell DATA;
#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, $data_position, 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, $data_position, 0;
print "Info: The position of DATA file handle is ", tell DATA, "\n";

#Approach 3
#Printing lines 2 through 5 using $.
$. = 0;
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 855
Info: The position of DATA file handle is 792
Srilanka
USA
Nepal
France
Info: The position of DATA file handle is 855
Info: The position of DATA file handle is 792
Srilanka
USA
Nepal
France
[/output]
 
best,
Shaji 
-------------------------------------------------------------------------------
Your talent is God's gift to you. What you do with it is your gift back to God.
-------------------------------------------------------------------------------



On Saturday, 26 October 2013 8:56 AM, John W. Krahn <jwkr...@shaw.ca> wrote:
 
Jim Gibson wrote:
>
> On Oct 25, 2013, at 4:46 PM, Shaji Kalidasan wrote:
>
>> 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.
>
> As you have discovered, the
 special file handle DATA is opened to the
>
 source file for your program. When the compiler encounters either of
> the following lines in your source file:
>
> __END__
> __DATA__
>
>
> it stops reading the source file and leaves the file handle open at
> that point. The first time you read the<DATA>  file handle, you get
> the line after the '__DATA__' line. But when you rewind the file
> handle, it is positioned at the beginning of the file, and you get to
> read your program.
>
> You have two choices (at least):
>
> 1. Save the position of the file handle when you enter the program:
>
>    my $data_pos = tell(<DATA>);

<DATA> is the same as readline( DATA ) which will not work with tell().

It should be just:

    my $data_pos = tell DATA;



John
-- 
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands,
 e-mail: beginners-h...@perl.org
http://learn.perl.org/

Reply via email to