sort multi-key hash by value and print out with key value pairs

A

Antonio Quinonez

I am trying to sort hash elements with the following structure:

%hashname{$npanxx}{$node}{$plan} = cost

so that i get plans ordered by lowest to greatest cost, in the format
of:
$npanxx:: $node :: $plan :: cost

i have the solution for SET 1(see below), where the second key ($node)
completes the hash member and gives me back what i want. I just can't
solve for the higher order hash. any help would be appreciated. sample
data for SET 1 and SET 2 and code for SET 1 follows:

%RS = ();

###SET 1
$RS{206231}{SEA} = 1.2;
$RS{206231}{NYC} = 11.2;
$RS{206231}{LAX} = 4.2;
$RS{206231}{CHI} = 10.2;

$RS{206254}{SEA} = 3.2;
$RS{206254}{NYC} = 11.2;
$RS{206254}{LAX} = 20.2;
$RS{206254}{CHI} = 15.2;


####SET 2
$RS{202211}{SEA}{plan_intel} = .2;
$RS{202211}{NYC}{plan_athlon} = 2;
$RS{202211}{LAX}{plan_duron} = 3;
$RS{202211}{CHI}{plan_pentium} = 2.5;

$RS{202200}{SEA}{plan_intel} =200;
$RS{202200}{NYC}{plan_athlon} = 20;
$RS{202200}{LAX}{plan_duron} =50;
$RS{202200}{CHI}{plan_pentium} = 100;


#### CODE ###
print "works for SET 1\n";
foreach $npanxx (sort keys %RS){
for $node (sort {$RS{$npanxx}{$a} <=> $RS{$npanxx}{$b}} keys %{
$RS{$npanxx} }) {
print "$npanxx :: $node :: $RS{$npanxx}{$node}\n";
}
print "\n";
}
 
S

Steven Kuo

I am trying to sort hash elements with the following structure:

%hashname{$npanxx}{$node}{$plan} = cost

so that i get plans ordered by lowest to greatest cost, in the format
of:
$npanxx:: $node :: $plan :: cost

i have the solution for SET 1(see below), where the second key ($node)
completes the hash member and gives me back what i want. I just can't
solve for the higher order hash. any help would be appreciated. sample
data for SET 1 and SET 2 and code for SET 1 follows:

%RS = ();

###SET 1
$RS{206231}{SEA} = 1.2;
$RS{206231}{NYC} = 11.2;
$RS{206231}{LAX} = 4.2;
$RS{206231}{CHI} = 10.2;

$RS{206254}{SEA} = 3.2;
$RS{206254}{NYC} = 11.2;
$RS{206254}{LAX} = 20.2;
$RS{206254}{CHI} = 15.2;


####SET 2
$RS{202211}{SEA}{plan_intel} = .2;
$RS{202211}{NYC}{plan_athlon} = 2;
$RS{202211}{LAX}{plan_duron} = 3;
$RS{202211}{CHI}{plan_pentium} = 2.5;

$RS{202200}{SEA}{plan_intel} =200;
$RS{202200}{NYC}{plan_athlon} = 20;
$RS{202200}{LAX}{plan_duron} =50;
$RS{202200}{CHI}{plan_pentium} = 100;


#### CODE ###
print "works for SET 1\n";
foreach $npanxx (sort keys %RS){
for $node (sort {$RS{$npanxx}{$a} <=> $RS{$npanxx}{$b}} keys %{
$RS{$npanxx} }) {
print "$npanxx :: $node :: $RS{$npanxx}{$node}\n";
}
print "\n";
}



Try:

my @values;
foreach my $npanxx (keys %RS) {
foreach my $node (keys %{$RS{$npanxx}}) {
foreach my $plan (keys %{$RS{$npanxx}{$node}}) {
push @values, [$npanxx, $node, $plan, $RS{$npanxx}{$node}{$plan}];
}
}
}


print map { (join " :: ", @$_[0..3]) . "\n" }
sort {$a->[3] <=> $b->[3]} @values;
 
A

Antonio Quinonez

i'm answering my own question. excuse me. essentially, i take a
multidimensional hash and i make it smaller dimensional.

in case anyone ever cares, again:

foreach $npanxx ( keys %RS ) {

for $node ( keys %{ $RS{$npanxx} } ) {
for $plan ( keys %{ $RS{$npanxx}{$node} } ) {
$newkey = "${node}:${plan}";
$RT{$npanxx}{$newkey} = $RS{$npanxx}{$node}{$plan};
}
}
}


foreach $npanxx (sort keys %RT){
for $np (sort {$RT{$npanxx}{$a} <=> $RT{$npanxx}{$b}} keys %{
$RT{$npanxx} }) {
print "$npanxx :: $np :: $RT{$npanxx}{$np}\n";
#then just break $np into $node and $plan using the split function on
":"
}
print "\n";
}

thanks for your attention.
aq
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top