I have a pairwise distances between objects stored as double hash (the keys 1 and 2 are stored in alphabetical order):

$distances{$key1}{$key2} = $value;

for example:

obj1 obj2 => 0.95 obj2 obj3 => 0.76 obj1 obj4 => 0.80 ...

I have initiated an array of hashes, so that the object names are keys and they point to the name of subset I want to group them into (five subsets in total), so that:

@subsets = ( { obj1 => "C1", obj2 => "C2", obj3 => "C3", obj4 => "C4", obj5 => "C5", } );

I also store already used object names in a separate hash:

%used = ( { obj1 => "C1", obj2 => "C2", obj3 => "C3", obj4 => "C4", obj5 => "C5", } );

Now I want to look up the highest pairwise distance for each object in $subsets[0] (value from %distances), and append the 'partner's' key to the $subsets[ 1], with the same value (C1-C5) (For instance, if the obj1, which points to C1 has the highest pairwise distance with obj6, then I want to store obj6 as key and C1 as value). Also, I need to add each new object to the %used, as to make sure if the pair has not been used before. Alternatively, I could remove the used up pairs from %distances.

for ($i = 0; $i < 50; $i++) { #loop over array of hashes (each subset +will have 50 keys) foreach my $key (keys $subsets[$i]) { #5 keys at each level my $max = 0; my @max; for my $key1 (keys %distances) { if $key !exists (keys %used) { $max = max (values %{$distances{$key1}}); push @max, $max; # somehow retrieve the 2nd key } } my $maxmax = max @max; # $subsets[$i+1]{2nd_key_with_max_val} = "e.g. C1" # push %used with key and value @max = []; } }

...but I clearly have problems making it work. Any ideas?


In reply to Separating hashed data into subsets by monkini

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.