my %unique; while ( ) { my ( $key, $value ) = split; $hash{ $key }{ $value } = (); } $_ = [ keys %$_ ] for values %hash; print Dumper \%hash;