my %hash; while () { chomp; my @foo = split /, /; push @{$hash{$foo[0]}}, 0+$foo[1]; } for (sort keys %hash) { print OUT join ', ', $_, @{$hash{$_}}; print OUT $/; }