sorting of hash of hash in perl

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nirmal1349
    New Member
    • Sep 2008
    • 9

    sorting of hash of hash in perl

    Hi,
    Lets say I have the following hash structure in perl:
    Code:
    my %products (
        1 => {
            name      => "Floor Wax",
            wholesale => "50.00",
            retail    => "abc1.doc",
            category => "abc",
        },
        2 => {
            name      => "Paper Towel",
            wholesale => "20.00",
            retail    => "abc2.doc",
            category => "abc",
        },
        3 => {
            name      => "Hand Soap",
            wholesale => "30.00",
            retail    => "xyz1.doc",
            category => "xyz",
       },
    );
     I want the o/p as :
     
       abc 
          abc1.doc
          abc2.doc
       xyz
          xyz1.doc
    pls tel me how will I perform sorting on this hash.
    Reply asap...
    Last edited by numberwhun; Sep 24 '08, 09:58 PM. Reason: Please use code tags
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    A hash has no order. You cannot sort the hash directly. What you can do is sort the keys of the hash (by getting the keys in a list using the keys function), and then access the hash with those keys in sorted order.

    You will need to write a short subroutine to say how you are sorting: i.e. you and not comparing the keys, but the field of the hash referred to by the key.

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      Have you tried to sort the data?

      reply ASAP....

      Comment

      • nirmal1349
        New Member
        • Sep 2008
        • 9

        #4
        hi,
        I tried sortin the data using smethng like this:
        Code:
        for my $key1 (reverse sort { $mydata{$b}{'Category'} cmp $mydata{$a}{'Category'} } keys %mydata) {....
        
        }
        here i can get the hash sorted using Category but then I need to go deep and further sort it by the retail value...
        i tried using diffrnt loops but cld not arive at the answer..
        Reply asap
        Last edited by numberwhun; Sep 24 '08, 10:54 PM. Reason: Please use code tags

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          one possible way:

          Code:
          my %products = (
          1 => {
          name => "Floor Wax",
          wholesale => "50.00",
          retail => "abc1.doc",
          category => "abc",
          },
          2 => {
          name => "Paper Towel",
          wholesale => "20.00",
          retail => "abc2.doc",
          category => "abc",
          },
          3 => {
          name => "Hand Soap",
          wholesale => "30.00",
          retail => "xyz1.doc",
          category => "xyz",
          },
          4 => {
          name => "Hand Soap",
          wholesale => "30.00",
          retail => "xyz2.doc",
          category => "xyz",
          },
          );
          
          my @sorted = map{"$_->[2] $_->[3] $_->[0] $_->[1]"}
                       sort{$a->[3] cmp $b->[3] || $a->[2] cmp $b->[2]} 
                       map{ [$products{$_}{'name'},$products{$_}{'wholsale'},$products{$_}{'retail'},$products{$_}{'category'}] } keys %products;
          
          print "$_\n" for @sorted;

          Comment

          Working...