473,326 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

sorting of hash of hash in perl

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
4 3195
Ganon11
3,652 Expert 2GB
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
4,059 Expert 2GB
Have you tried to sort the data?

reply ASAP....
Sep 24 '08 #3
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
4,059 Expert 2GB
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

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

Similar topics

2
by: pillbug | last post by:
Hi, I'm trying to tie a hash that is returned from a DBI method called fetchrow_hashref. All I want to do is override the FETCH method to validate my key names. But fetchrow_hashref returns...
2
by: Christopher | last post by:
I have a hash that is several levels deep. ie: 'vars' => { '$filers' => '10.10.10.10/32', '$networksa' => '10.10.10.10/32', '$networksb' => '10.50.0.0/16', '$wintel_boxes' =>...
20
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: ...
25
by: Dan Stromberg | last post by:
Hi folks. Python appears to have a good sort method, but when sorting array elements that are very large, and hence have very expensive compares, is there some sort of already-available sort...
12
by: DaveMM | last post by:
I am a novice perl programmer, and I am trying to sort a hash by a value in the hash and I am having problems. I've done this before, but this time something is going wrong and I'm at a loss. I'm...
5
KevinADC
by: KevinADC | last post by:
Introduction This discussion of the sort function is targeted at beginners to perl coding. More experienced perl coders will find nothing new or useful. Sorting lists or arrays is a very common...
1
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
3
KevinADC
by: KevinADC | last post by:
If you are entirely unfamiliar with using Perl to sort data, read the "Sorting Data with Perl - Part One and Two" articles before reading this article. Beginning Perl coders may find this article...
12
by: dillipkumar | last post by:
Hi, I have a Hash: %hash= 'student' => , I want to sort the hash according to sorting of 'roll_no' .. After sorting It should be displayed %hash= 'student' => ,
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.