use Set::Scalar; my %hash; while () { chomp($_); my ($first, $second) = split(/s+/, $_); if (!exists $hash{$first}) { $hash{$first} = new Set::Scalar(); } $hash{$first}->insert($second); } foreach my $key (keys %hash) { print "The Key is: $key\n\t", join("\n\t", $hash{$key}->members()), "\n"; }