in reply to Re^4: Tie::Hash::MultiValue Unique question
in thread Tie::Hash::MultiValue Unique question
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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Tie::Hash::MultiValue Unique question
by elsubliminal (Initiate) on Apr 18, 2009 at 07:15 UTC | |
by ikegami (Patriarch) on Apr 18, 2009 at 18:54 UTC | |
by elsubliminal (Initiate) on Apr 19, 2009 at 10:34 UTC |