JayDog,
If your goal is to place as many invoices into a single file up to a certain size limit and ones that exceed that size limit in their own file, then this is how I would do it (untested):
#!/usr/bin/perl use constant CHUNK => 500 * 1024; # 500 KB use strict; use warnings; use List::Util 'sum'; my $file = $ARGV[0] or die "Usage: $0 <input_file>"; open(my $in_fh, '<', $file) or die "Unable to open '$file' for reading +: $!"; my ($cnt, @buffer); while (<$in_fh>) { if (is_new_invoice($_)) { my $size = sum(map length($_), @buffer); if ($size > CHUNK) { flush_buffer(); push @buffer, $_; } else { push @buffer, $_; } } else { $buffer[-1] .= $_; } } flush_buffer(); sub flush_buffer { # variables should probably be passed explicitly return if ! @buffer; ++$cnt; open ($out_fh, '>', "outfile.$cnt") or die "Unable to open 'outfil +e.$cnt' for writing: $!"; my $size = 0; while (1) { my $invoice = shift @buffer; last if ! $invoice; my $len = length($invoice); # If invoice by itself is larger than chunk, write to new file if ($len > CHUNK) { ++$cnt; open ($out_fh, '>', "outfile.$cnt") or die "Unable to open + 'outfile.$cnt' for writing: $!"; print $out_fh $invoice; close $out_fh; next; } # If this $invoice puts the current file over the limit, close + the current file if ($size + $len > CHUNK) { close $out_fh; # Replace the invoice back in the buffer and return if not + end of input if (! eof $in_fh) { unshift @buffer, $invoice; } else { # Write out whatever is left ++$cnt; open ($out_fh, '>', "outfile.$cnt") or die "Unable to +open 'outfile.$cnt' for writing: $!"; print $out_fh $invoice; close $out_fh; } return; } else { # Add this invoice to the existing open file print $out_fh $invoice; } last if ! @buffer; } } sub is_new_invoice { my ($line) = @_; return 1 if substr($line, 66, 2) eq '11'; return 0; }

Cheers - L~R


In reply to Re: Removing Large Invoices from a Data File by Limbic~Region
in thread Removing Large Invoices from a Data File by JayDog

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.