should do what you want. The list references that are the value of the hash are instantiated autmagically if necessary.my %hash; while (<DATA>) { chomp($_); my ($first, $second) = split(/s+/, $_); push(@{$hash{$first}}, $second); } foreach my $key (keys %hash) { print "The $key is: ", join(', ', @{$hash{$key}}), "\n"; }
Hope this helps, -gjb-
Update: Sorry, missed the uniqueness requirement. Since a number of good solutions have already been proposed I'll just give an alternative one. (I love sets.)
use Set::Scalar; my %hash; while (<DATA>) { 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"; }
In reply to Re: Unique keys for multiple values with a Hash
by gjb
in thread Unique keys for multiple values with a Hash
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |