However, I do not want the array to be greater than 24 kilobytes in size.
I assume here you mean that when the file is written to disk it will take up no more than 24kb as the actual size of the arrray in memory will be greater than 24kb due to the extra information perl associates with any data.

If you're going for exact sizes and you're not too worried about the lines you could just set the $/ var to 24576 and read the file in normally e.g

open(my $fh, "mahoosive_file") or die("ack - $!"); my @chunks; { local $/ = \24576; push @chunks, $_ while <$fh>; }
Now each element of @chunks will contains a chunk of the file upto 24kb in size. However if you must maintain the lines then you'll have to do some funky file pointer re-positioning
... { # NOTE: code is untested local $/ = \24576; while(<$fh>) { my $chunk = $_; my $last_rs = rindex($chunk, $/) push @chunks, substr($chunk, 0, $last_rs); seek($fh, 1, -(length($chunk) - $last_rs)); } }

HTH

_________
broquaint


In reply to Re: Controlling the Size of an Array based on its file source. by broquaint
in thread Controlling the Size of an Array based on its file source. by neilwatson

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.