in reply to Controlling the Size of an Array based on its file source.
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
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-positioningopen(my $fh, "mahoosive_file") or die("ack - $!"); my @chunks; { local $/ = \24576; push @chunks, $_ while <$fh>; }
... { # 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)); } }
_________
broquaint
|
|---|