Rather than reading the entire input file into memory (e.g., as hash of lists) and then emitting your output, you can emit output in passing, as soon as each output line is determined. This approach has the advantage of requiring very little memory, which is important if your input files can be large.
Here's one possible implementation, which uses autosplitting and other handy command-line flags (see perlrun):
Because of the command-line switches we used, the body of the code will be run for each line of input, and the following variables will be set up for us automatically:#!/usr/bin/perl -lanF, # if the current key (in $F[0]) is not the same as the # last key we saw, print out the merged output line for # the last key and then start a new merged output # line for the current key if ($last_key ne $F[0]) { print $merged if $merged; $merged = $_; $last_key = $F[0]; } # otherwise, the current key is the same as the last, # and so we can merge this line's value portion (in # $F[1]) with the previous else { $merged .= ",$F[1]"; } # when we reach the end of the file, we must print # the final merged output line END { print $merged if $merged }
Hope this helps.
$_ = the entire input line, with linefeed stripped $F[0] = the key portion of the line $F[1] = the value portion of the line
Cheers,
Tom
Tom Moertel : Blog / Talks / CPAN / LectroTest / PXSL / Coffee / Movie Rating Decoder
In reply to Re: Reformat Text File
by tmoertel
in thread Reformat Text File
by arunhorne
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |