Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^2: how to read multiple line from a file

by educated_foo (Vicar)
on Apr 13, 2013 at 13:27 UTC ( [id://1028515]=note: print w/replies, xml ) Need Help??


in reply to Re: how to read multiple line from a file
in thread how to read multiple line from a file

Just a minor note from the C programmer in me: the easiest and most efficient way to do a fixed-size queue is to use the mod operator:
while (<>) { $buf[$. % $size] = $_; }

Replies are listed 'Best First'.
Re^3: how to read multiple line from a file
by LanX (Saint) on Apr 13, 2013 at 14:01 UTC
    As a minor note of a functional programmer, I'll put the logic in an extra iterator like here: Re: Split string after 14 Line Feeds?.

    This makes the code much more readable. =)

    Otherwise I'm not sure where you want the processing logic to happen in your code, maybe something like unless ($. % $size) { ...} within the loop?

    I think If a sliding window is needed, shift and push might be better.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      If a sliding window is needed, shift and push might be better.
      This leads to extra memmove()s if you're lucky (I believe Perl does this), and endless memory consumption otherwise. If you use the mod operator when indexing your array, you get a ring buffer without excess copying. You could probably abstract this out nicely with a bit of extra work.
      Just another Perler interested in Befunge Programming.
        > This leads to extra memmove()s if you're lucky (I believe Perl does this),

        Sure but I think we should try and measure before we optimize.

        > and endless memory consumption otherwise.

        Why? Someone .. (I think Grandfather) said once linked lists are rarely needed in Perl, because arrays are optimized in this way.(?!?)

        Anyway having an array were the newest line is somewhere in the middle results in arithmetical overhead.

        Slicing and copying this array in a sorted one leads to new overhead.

        I think try and measure is easier than digging into theoretical calculations...

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Re^3: how to read multiple line from a file
by ww (Archbishop) on Apr 14, 2013 at 00:13 UTC
    How does this adapt to variable length records/lines/data?

    If you didn't program your executable by toggling in binary, it wasn't really programming!

      It keeps a fixed number of records (as defined by $/). Length has nothing to do with it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1028515]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-03-28 15:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found