You are worried about the memory requirements of stuffing the file into a hash, but if the file fits into memory you could stuff it into an array and then use
grep to get code which looks a lot cleaner:
use strict;
open FILE, "<untested.txt" or die "Blerch: $!\n";
my @contents=<FILE>;
close FILE;
open OUTPUT ">outputfile" or die "Hcrelb: $!\n";
for my $d (1..10) {
print OUTPUT join $\, grep (/^$d/, @contents);
}
close OUTPUT;
Whether your second requirement is fullfilled with this (lines are written in same order as in inputfile) depends on whether or not grep
retains preserves ordering
in its input. A quick test on my machine here has indicated that it does, but I don't know whether that's just luck or whether grep is supposed to do it like that.
Also note that the above code is untested.
And to top it all off, you can do this even if your file does not fit into memory: use Tie::File to tie the @contents array to the file and away you go.
CU
Robartes-
Update: Clarified things a bit.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.