Update: Corion ++ pointed out that I reversed the sense of your problem, sorry for my confusion.

Instead, have your wrapper script read 1000 lines from foo, put them in a temporary file and then invoke X. Repeat till done.

#! perl -sw use strict; open FOO, '<', 'foo' or die $!; while( not eof( FOO ) ) { my @lines; push @lines, scalar <FOO> for 1..1000; open TEMPFOO, '>tempfoo' or die $!; print TEMPFOO @lines; close TEMPFOO; qx[ x --filename:tempfoo -firstline:1 -lastline:1000 ]; } close FOO;

Instead of having X supply you 1000 lines at a time, ask for them all and pipe the result to your perl script. In your perl script, read 1000 lines from the pipe into an array, process them, then loop back and get the next 1000 lines. Repeat till done.

The output of X will be blocked while your script processes each batch of 1000 lines. Your script will only ever have to hold 1000 lines in memory at a time. X will never have to backtrack or skip over any lines.

A silly example. (One liner wrapped for display)

perl -ne"print" junk | perl -le" while( not eof(STDIN) ) { $,=' '; push @a, scalar <> for 1..10; chomp @a; print reverse @a; @a=(); }" #Outputs 10 9 8 7 6 5 4 3 2 1 20 19 18 17 16 15 14 13 12 11 30 29 28 27 26 25 24 23 22 21 40 39 38 37 36 35 34 33 32 31 50 49 48 47 46 45 44 43 42 41 60 59 58 57 56 55 54 53 52 51 70 69 68 67 66 65 64 63 62 61 80 79 78 77 76 75 74 73 72 71 90 89 88 87 86 85 84 83 82 81 100 99 98 97 96 95 94 93 92 91 110 109 108 107 106 105 104 103 102 101 ...

The first instance of perl just reads the file junk (one integer per line), and prints it to stdout. The second instance, loops, reading 10 lines, chomping them, reversing them and printing them before emptying the array and going back for the next 10.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
Hooray!
Wanted!


In reply to Re: splitting an input stream by BrowserUk
in thread splitting an input stream by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.