Oh! You keep talking about avoiding duplicates in a hash, but your problem is actually with avoiding duplicates in an array.
A common idiom to weed out duplicates is: (from perlfaq4)
my %seen; my @array = grep !$seen{$_}++, LIST;
To insert at the same time, that would be
my %seen; my @array = grep !$seen{$_}++, @array, $new_value;
Plug it in and get:
my %edges; while (<STREAM>) { my($node1, $node2) = split; my %seen; @{$edges{$node1}} = grep !$seen{$_}++, @{$edges{$node1}}, $node2; }
A faster (but more memory hungry) version would be:
my %edges; my %seen; while (<STREAM>) { my($node1, $node2) = split; next if !$seen{$node1}{$node2}++; push @{$edges{$node1}}, $node2; }
In reply to Re^5: Tie::Hash::MultiValue Unique question
by ikegami
in thread Tie::Hash::MultiValue Unique question
by elsubliminal
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |