my %hash = (); while () { # split based on spaces my ($hex, $foo, $int) = split /\s+/; $hash{$hex}{$int}++; } for (keys %hash) { print "$_ = ", join(", ", sort keys %{$hash{$_}}), "\n"; }