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

Discussion in 'Perl Misc' started by Antonio Quinonez, Aug 14, 2003.

  1. 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";
    }
     
    Antonio Quinonez, Aug 14, 2003
    #1
    1. Advertising

  2. Antonio Quinonez

    Steven Kuo Guest

    On 14 Aug 2003, Antonio Quinonez wrote:

    > 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;


    --
    Hope this helps,
    Steven
     
    Steven Kuo, Aug 14, 2003
    #2
    1. Advertising

  3. 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

    (Antonio Quinonez) wrote in message news:<>...
    > 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";
    > }
     
    Antonio Quinonez, Aug 14, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Markus Dehmann

    key-value pairs: key consists of 3 ints

    Markus Dehmann, Jan 15, 2006, in forum: C++
    Replies:
    13
    Views:
    657
    Richard Herring
    Jan 23, 2006
  2. Une bévue
    Replies:
    5
    Views:
    179
    Une bévue
    Aug 10, 2006
  3. Aryk Grosz
    Replies:
    13
    Views:
    249
    Trans
    Nov 18, 2008
  4. Mmcolli00 Mom
    Replies:
    5
    Views:
    150
    Mmcolli00 Mom
    Jan 12, 2009
  5. Johan Martinez
    Replies:
    3
    Views:
    302
    Johan Martinez
    May 22, 2011
Loading...

Share This Page