use strict; 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"; } __DATA__ 0x130005d 1.253.54.1 11 0x130009c 1.253.54.2 12 0x130005d 1.253.54.1 14 0x130005d 1.253.54.1 11 0x130005d 1.253.54.1 10 0x130009c 1.253.54.2 12 0x130009c 1.253.54.2 14