Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

How to read every 16 line of file ?

by bh_perl (Monk)
on May 13, 2011 at 02:39 UTC ( #904573=perlquestion: print w/replies, xml ) Need Help??

bh_perl has asked for the wisdom of the Perl Monks concerning the following question:


Could somebody help me how to read every 16 line from the input file ?



Replies are listed 'Best First'.
Re: How to read every 16 line of file ?
by ikegami (Patriarch) on May 13, 2011 at 02:42 UTC
      You forgot to reset line numbers across file boundaries.
        FWIW, if reading from STDIN, there are no file boundaries :)
Re: How to read every 16 line of file ?
by davido (Cardinal) on May 13, 2011 at 05:28 UTC

    ikegami really proposed the easiest solution; file line number (represented in $.), modulus 16. If there's no "remainder", you're on a line that is a multiple of 16. You could, as an alternative for math-challenged individuals, increment a counter, and when it gets to the trigger value reset it and print the line. But that's more work just for the sake of being lazy. ;)

    Oh, that reminds me of why I replied to this post (after an obviously perfect answer has already been given)... You are asking how to read every 16th line, but you really should be asking how to ignore all but every 16th line. Or in other words, read every line but only take action on multiples of 16. There is no easy way to only read every 16th line, if lines are of unknown length. That's a little like asking how to find "steam" in the dictionary without thumbing through it to find it.

    There is the other lazy and inefficient alternative of using Tie::File to gain an index to each line, but then you still have to do math, so it really doesn't buy you anything other than the ability to easily access any line at will.


Re: How to read every 16 line of file ?
by LanX (Sage) on May 13, 2011 at 14:01 UTC
    I think what you want is reading 16 lines in a gulp not every 16th line.

    Cheers Rolf


    more explicit:

    while ( @lines = readlines($filehandle,16) ) { print @lines,"----\n"; } sub readlines { my ($fh, $count) = @_; my @gulp; while (<$fh>) { push @gulp,$_; last unless --$count; } return @gulp; }
Re: How to read every 16 line of file ?
by ikegami (Patriarch) on May 13, 2011 at 16:31 UTC

    If I misunderstood and you are trying to get blocks of 16 lines,

    my @buf; while (<>) { push @buf, $_; if (@buf == 16) { do_it(\@buf); @buf = (); } } if (@buf) { do_it(\@buf); }
Re: How to read every 16 line of file ?
by anonymized user 468275 (Curate) on May 13, 2011 at 13:06 UTC
    perl -ne 'if ( ++$x == 16 ){ print $_; $x = 0; }' <filename

    Update: but only if the rest of the logic is simple enough for inclusion in a one-liner like this.

    update2: modified to shift target line nos to 16,32 etc. - a weird brainbug made me think 1,17 etc. was wanted - I need a holiday, lol!

    One world, one people

Re: How to read every 16 line of file ?
by monk2b (Pilgrim) on May 13, 2011 at 15:27 UTC
    my $line_number = 16; my $count = 0; while(<DATA>){ chomp($_); $count++; next unless $count == $line_number; print "$_\n"; $count = 0; } __DATA__ 1 Bob 48 IT 2 David 26 IT 3 Jerry 22 IT 4 Josh 33 IT 5 Karl 35 IT 6 Dan 50 IT 7 Tom 26 IT 8 Randy 48 Engineering 9 Trevor 57 Engineering 10 Tony 48 Engineering 11 Larry 39 Engineering 12 Farhan 40 Engineering 13 Sandy 28 Engineering 14 Jessie 55 Engineering 15 Carl 25 Engineering 16 Donald 58 Engineering 17 Rick 44 Engineering 18 Barry 38 Engineering 19 Bruce 30 Engineering 20 Joe 37 Engineering 21 Mun 53 Engineering 22 Shiro 48 Engineering 23 Sally 50 HR 24 John 32 Finance
    Perl Good - Manual process bad
Re: How to read every 16 line of file ?
by Anonymous Monk on May 15, 2011 at 01:09 UTC
    Just read them all and discard 15 out of 16. You really do not need to get any more clever than that. The way the processing actually will work, with disk buffers and all that, being smarter won't make the slightest bit of difference, and might well make it slower.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://904573]
Approved by ikegami
Front-paged by davido
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2022-07-02 13:48 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (103 votes). Check out past polls.