in reply to Reading a huge input line in parts

How about reading a block at a time and spliting that?:

sub genBufferedGetNum { my @buf = do{ local $/ = \4096; split ' ', scalar <>; } my $leftover = pop @buf; return sub { unless( @buf ) { unless( eof ) { @buf = do{ local $/ = \4096; split ' ', $leftover . <> + }; $leftover = pop @buf; } else { die 'premature eof' if $leftover != 0; return $leftover; # last number } } return shift @buf; }; } my $getNum = genBufferedGetNum(); while( my $num = getNum->() ) { ## do stuff }

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
  • Comment on Re: Reading a huge input line in parts (Handles multi-digit numbers!)
  • Download Code

Replies are listed 'Best First'.
Re^2: Reading a huge input line in parts (Handles multi-digit numbers!)
by GotToBTru (Prior) on May 04, 2015 at 19:19 UTC

    Could this not be simplified?

    sub genBufferedGetNum { return sub { @buf = do{ local $/ = \10; split ' ', <> }; return @buf; }; } my $getNum = genBufferedGetNum(); while( my @part = $getNum->() ) { print @part, "\n"; }

    I shortened the buffer size for testing purposes

    $: cat tb.dat 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 $:
    $: cat tb.dat | perl tb.pl 01234 56789 01234 56789 01234 56789 0
    Dum Spiro Spero
      Could this not be simplified?

      And what happens when your buffer size splits a multi-digit number in two?

      Ie. Run your code against this input:

      123 456 789 1

      And it produces: 123 456 78 9 1

      And doesn't notice that the last number is supposed to be 0.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
      In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked