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

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)

Replies are listed 'Best First'.
Re^4: how to read multiple line from a file
by educated_foo (Vicar) on Apr 14, 2013 at 18:11 UTC
    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)

        I think try and measure is easier than digging into theoretical calculations...
        This has nothing to do with theory. An array is a contiguous chunk of memory. If you put things on one side and take them from the other, you can either shift the current contents, or keep allocating more memory.
        Just another Perler interested in Befunge Programming.