in reply to Dealing with nextline of a file

Adapting my reply to Reading files n lines a time illustrates another approach:

use strict; use warnings; my $fname = shift or die "usage: $0 fname\n"; open(my $fh, '<', $fname) or die "error: open '$fname': $!"; my $chunksize = 2; my $chunk = ""; while ( my $line = <$fh> ) { $chunk .= $line; next if $. % $chunksize; print "---chunk---\n$chunk"; $chunk = ""; } close $fh; if (length $chunk) { print "---chunk (leftover)---\n$chunk"; }

Sample output with two simple test files, one with four lines:

---chunk--- one two ---chunk--- three four

and one with five lines:

---chunk--- one two ---chunk--- three four ---chunk (leftover)--- five

For your use-case you would obviously do something different to my crude printing of chunks above.

Some Similar PM Nodes