in reply to Reading Text Lines

The basic idea is to have the number of the first line that you want to display and use $. to work out where you are in the file.

my $page = 1; # which page to display my $display_perl_page = 100; my $first = ($page - 1) * $display_perl_page; my $last = $first + $display_perl_page; while (<VIEWFILE>) { next unless $. >= $first; last if $. > $last; # display this record }

(That code is untested, there may be an off-by-one error)

There are modules like Data::Page that make this all a lot easier.

Another alternative might be Tie::File.

--
<http://dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

Replies are listed 'Best First'.
Re^2: Reading Text Lines
by Fletch (Bishop) on May 11, 2006 at 16:54 UTC

    And if you're going to do this kind of thing frequently it may make sense to build an index file with line numbers mapped to offsets from tell and then use seek to jump straight to that location in the file.