in reply to Reformat Text File
Here's a more memory efficient solution that only works if the keys are already sorted:
{ my $last; my @list; local $, = ','; local $\ = $/; while (<>) { chomp; my ($key, $val) = split($,, $_, 2); print $last, splice(@list) if ($.!=1 && $key ne $last); $last = $key; push(@list, $val); } print $last, @list if (@list); }
I like the symetry of $/ and $, being used for both input and output.
|
|---|