in reply to rearranging text

Collect your data in a hash of arrays, pushing on the data as it is seen,

my %hash; while (<IN>) { chomp; my @foo = split /, /; push @{$hash{$foo[0]}}, 0+$foo[1]; } for (sort keys %hash) { print OUT join ', ', $_, @{$hash{$_}}; print OUT $/; }
The 0+$foo[1] is there to make the value numeric and so strip trailing whitespace.

After Compline,
Zaxo