By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,708 Members | 2,095 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,708 IT Pros & Developers. It's quick & easy.

sorting of hash of hash in perl

P: 9
Hi,
Lets say I have the following hash structure in perl:
Expand|Select|Wrap|Line Numbers
  1. my %products (
  2.     1 => {
  3.         name      => "Floor Wax",
  4.         wholesale => "50.00",
  5.         retail    => "abc1.doc",
  6.         category => "abc",
  7.     },
  8.     2 => {
  9.         name      => "Paper Towel",
  10.         wholesale => "20.00",
  11.         retail    => "abc2.doc",
  12.         category => "abc",
  13.     },
  14.     3 => {
  15.         name      => "Hand Soap",
  16.         wholesale => "30.00",
  17.         retail    => "xyz1.doc",
  18.         category => "xyz",
  19.    },
  20. );
  21.  I want the o/p as :
  22.  
  23.    abc 
  24.       abc1.doc
  25.       abc2.doc
  26.    xyz
  27.       xyz1.doc
  28.  
pls tel me how will I perform sorting on this hash.
Reply asap...
Sep 24 '08 #1
Share this Question
Share on Google+
4 Replies


Ganon11
Expert 2.5K+
P: 3,652
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.
Sep 24 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
Have you tried to sort the data?

reply ASAP....
Sep 24 '08 #3

P: 9
hi,
I tried sortin the data using smethng like this:
Expand|Select|Wrap|Line Numbers
  1. for my $key1 (reverse sort { $mydata{$b}{'Category'} cmp $mydata{$a}{'Category'} } keys %mydata) {....
  2.  
  3. }
  4.  
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
Sep 24 '08 #4

KevinADC
Expert 2.5K+
P: 4,059
one possible way:

Expand|Select|Wrap|Line Numbers
  1. my %products = (
  2. 1 => {
  3. name => "Floor Wax",
  4. wholesale => "50.00",
  5. retail => "abc1.doc",
  6. category => "abc",
  7. },
  8. 2 => {
  9. name => "Paper Towel",
  10. wholesale => "20.00",
  11. retail => "abc2.doc",
  12. category => "abc",
  13. },
  14. 3 => {
  15. name => "Hand Soap",
  16. wholesale => "30.00",
  17. retail => "xyz1.doc",
  18. category => "xyz",
  19. },
  20. 4 => {
  21. name => "Hand Soap",
  22. wholesale => "30.00",
  23. retail => "xyz2.doc",
  24. category => "xyz",
  25. },
  26. );
  27.  
  28. my @sorted = map{"$_->[2] $_->[3] $_->[0] $_->[1]"}
  29.              sort{$a->[3] cmp $b->[3] || $a->[2] cmp $b->[2]} 
  30.              map{ [$products{$_}{'name'},$products{$_}{'wholsale'},$products{$_}{'retail'},$products{$_}{'category'}] } keys %products;
  31.  
  32. print "$_\n" for @sorted;
Sep 24 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.