in reply to Read File In Four-Line Chunks / TMTOWTDI / Golf
So you can treat a read_chunk() operation as an atomic one.use strict; use warnings; while ( my @s = read_chunk() ) { print join " | ", @s; print "\n"; } sub read_chunk { my @stuff = (); while( my $line = <DATA> ) { chomp $line; push @stuff, $line; last if $. % 4 == 0; } return @stuff; } __DATA__ first1 second1 third1 fourth1 first2 second2 third2 fourth2 first3 second3
A more clever option you could explore is using Tie::File, which allows you to manipulate the file as an array of lines, even if simgle lines are actually read only when they're needed (details on the documentation).
|
|---|