use strict; use warnings; my %thingy; while () { chomp; my ($index,$value) = split; push @{$thingy{$index}}, $value; } for (sort keys %thingy) { print "$_ ", join('-',@{$thingy{$_}}), "\n"; } __DATA__ 1 0 1 1 2 0 3 1 4 3 4 6