monkini has asked for the wisdom of the Perl Monks concerning the following question:
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Separating hashed data into subsets
by roboticus (Chancellor) on Nov 11, 2013 at 15:34 UTC | |
|
Re: Separating hashed data into subsets
by hdb (Monsignor) on Nov 11, 2013 at 15:35 UTC |