Something like this may do what you want:
use warnings; use strict; use Data::Dumper; my %edges; # created empty while (<DATA>) { my ($node1, $node2) = split; # nothing to do if node1 already has a node2 in its array. next if exists $edges{$node1}{present}{$node2}; # node2 not already there: push it, mark it present. push @{ $edges{$node1}{array} }, $node2; $edges{$node1}{present}{$node2} = 1; } # transform hash to final structure. for my $node (keys %edges) { # for each node1 of hash... # presence flags no longer needed: eliminate them. delete $edges{$node}{present}; # associate each node1 directly with its node2 array ref. $edges{$node} = delete $edges{$node}{array}; } # see what we got. print Dumper \%edges; __DATA__ 1 2 1 3 1 4 1 2 1 5 2 1 1 2 2 2 1 6 2 3 1 2 2 2
Output:
>perl uniq_hash_value_1.pl $VAR1 = { '1' => [ '2', '3', '4', '5', '6' ], '2' => [ '1', '2', '3' ] };

In reply to Re^5: Tie::Hash::MultiValue Unique question by AnomalousMonk
in thread Tie::Hash::MultiValue Unique question by elsubliminal

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.