Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Reading files n lines a time

by eyepopslikeamosquito (Chancellor)
on Dec 07, 2012 at 03:02 UTC ( #1007676=note: print w/replies, xml ) Need Help??

in reply to Reading files n lines a time

Here's a simple way to do it using modulo chunksize on the line number $.

use strict; use warnings; my $fname = shift or die "usage: $0 fname\n"; open(my $fh, '<', $fname) or die "error: open '$fname': $!"; my $chunksize = 4; 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"; }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1007676]
[Discipulus]: choroba's tk cb client become incerdibly stable after https switch; it's a coincidence?
[Discipulus]: it hits some read timeouts but reconnect without issues
erix googles incerdibly and finds incredibly (translate is cleverder than I am)
[Discipulus]: found translate only good from or possibly to english.. in any other case i use double way translation

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2018-06-19 07:32 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (111 votes). Check out past polls.