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

sorting array of hash in hash

P: 41
Hi,
I have a Hash:

Expand|Select|Wrap|Line Numbers
  1. %hash= 'student'  => [
  2.                                     {
  3.                                        'roll_no' => 10,
  4.                                        'sub' => 'eng'
  5.                                        marks => 32,
  6.                                     },
  7.                                     {
  8.                                        'roll_no' => 11,
  9.                                        'sub' => 'math'
  10.                                        marks => 69,
  11.                                     },
  12.                                     {
  13.                                        'roll_no' => 10
  14.                                        'sub' => 'science'
  15.                                        marks => 69,
  16.                                     },
  17.  
  18.                                     {
  19.                                        'roll_no' => 25
  20.                                        'sub' => 'geo'
  21.                                        marks => 59
  22.                                     },
  23.  
  24.                                 ],

I want to sort the hash according to sorting of 'roll_no' ..
After sorting It should be displayed

Expand|Select|Wrap|Line Numbers
  1. %hash= 'student'  => [
  2.                                     {
  3.                                        'roll_no' => 10,
  4.                                        'sub' => 'eng'
  5.                                        marks => 32,
  6.                                     },
  7.                                      {
  8.                                        'roll_no' => 10
  9.                                        'sub' => 'science'
  10.                                        marks => 69,
  11.                                     },
  12.  
  13.                                      {
  14.                                        'roll_no' => 11,
  15.                                        'sub' => 'math'
  16.                                        marks => 69,
  17.                                     },
  18.  
  19.                                     {
  20.                                        'roll_no' => 25
  21.                                        'sub' => 'geo'
  22.                                        marks => 59
  23.                                     },
  24.  
  25.                                 ],
Can anybody help me...
Mar 31 '08 #1
Share this Question
Share on Google+
12 Replies


KevinADC
Expert 2.5K+
P: 4,059
Is this school/class/course work?
Mar 31 '08 #2

Ganon11
Expert 2.5K+
P: 3,652
A hash has no order. If you want to have data sorted, use an array instead.
Mar 31 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
A hash has no order. If you want to have data sorted, use an array instead.
It is possible but I would also recommend using an array. See Tie::SortHash and Tie::IxHash.
Mar 31 '08 #4

P: 41
Hi,
Actually this is a task..
The Original Hash is like this:
Expand|Select|Wrap|Line Numbers
  1. %hash = ( 'difference' => [
  2.                                 {
  3.                                   'line_number' => '21',
  4.                                   'filename' => 'BRANCH27',
  5.                                   'source_line' => {},
  6.                                   'target_line' => '001S TR'
  7.                                 },
  8.                                 {
  9.                                   'line_number' => '1',
  10.                                   'filename' => 'BRANCH',
  11.                                   'source_line' => {},
  12.                                   'target_line' => '023X '
  13.                                 },
  14.                                 {
  15.                                   'line_number' => '140',
  16.                                   'filename' => 'BRANCH',
  17.                                   'source_line' => {},
  18.                                   'target_line' => '139FIFTH' 
  19.                                 },
  20.                                 {
  21.                                   'line_number' => '1',
  22.                                   'filename' => 'ACCOUNT27',
  23.                                   'source_line' => {},
  24.                                   'target_line' => '0223X '
  25.                                 },
  26.                              ], 
  27.  
  28.              );
i want to sort this hash..
Apr 1 '08 #5

P: 41
A hash has no order. If you want to have data sorted, use an array instead.

Hi The Original hash is like this:
Expand|Select|Wrap|Line Numbers
  1. %hash = ( 'difference' => [
  2.                                 {
  3.                                   'line_number' => '21',
  4.                                   'filename' => 'BRANCH27',
  5.                                   'source_line' => {},
  6.                                   'target_line' => '001S TR'
  7.                                 },
  8.                                 {
  9.                                   'line_number' => '1',
  10.                                   'filename' => 'BRANCH',
  11.                                   'source_line' => {},
  12.                                   'target_line' => '023X '
  13.                                 },
  14.                                 {
  15.                                   'line_number' => '140',
  16.                                   'filename' => 'BRANCH',
  17.                                   'source_line' => {},
  18.                                   'target_line' => '139FIFTH' 
  19.                                 },
  20.                                 {
  21.                                   'line_number' => '1',
  22.                                   'filename' => 'ACCOUNT27',
  23.                                   'source_line' => {},
  24.                                   'target_line' => '0223X '
  25.                                 },
  26.                              ], 
  27.  
  28.              );
Apr 1 '08 #6

nithinpes
Expert 100+
P: 410
Hi The Original hash is like this:
%hash = ( 'difference' => [
{
'line_number' => '21',
'filename' => 'BRANCH27',
'source_line' => {},
'target_line' => '001S TR'
},
{
'line_number' => '1',
'filename' => 'BRANCH',
'source_line' => {},
'target_line' => '023X '
},
{
'line_number' => '140',
'filename' => 'BRANCH',
'source_line' => {},
'target_line' => '139FIFTH'
},
{
'line_number' => '1',
'filename' => 'ACCOUNT27',
'source_line' => {},
'target_line' => '0223X '
},
],

);
The structure is hash of array of hashes. To display it sorted according to 'line_number' foreach top-level key('difference'), you would be actually sorting the array and not the hash (according to the intended output you posted in your initial description).
You can use:
Expand|Select|Wrap|Line Numbers
  1. foreach $k (keys %hash) {
  2. print "\n\n$k \n\n";
  3.  foreach $a (sort {$a->{'line_number'} <=> $b->{'line_number'}} @{$hash{$k}}) {
  4.   print "$_   : $a->{$_}\n" foreach(keys %{$a});
  5.   print "\n";
  6.    }
  7. }
  8.  
Apr 1 '08 #7

P: 41
The structure is hash of array of hashes. To display it sorted according to 'line_number' foreach top-level key('difference'), you would be actually sorting the array and not the hash (according to the intended output you posted in your initial description).
You can use:
Expand|Select|Wrap|Line Numbers
  1. foreach $k (keys %hash) {
  2. print "\n\n$k \n\n";
  3.  foreach $a (sort {$a->{'line_number'} <=> $b->{'line_number'}} @{$hash{$k}}) {
  4.   print "$_   : $a->{$_}\n" foreach(keys %{$a});
  5.   print "\n";
  6.    }
  7. }
  8.  
Hi Nitin,

Now it is working but the 'source_line' returns the hash like:
source_line : HASH(0x81ffdac)
Apr 1 '08 #8

nithinpes
Expert 100+
P: 410
Hi Nitin,

Now it is working but the 'source_line' returns the hash like:
source_line : HASH(0x81ffdac)
That is because you have an empty hash as value for 'source_line' key.
Apr 1 '08 #9

P: 41
That is because you have an empty hash as value for 'source_line' key.
Actually after executing it is displaying this format -->
Expand|Select|Wrap|Line Numbers
  1. {
  2. filename' => 'BRANCH27',
  3. line_number' => '21',
  4. 'source_line' => {},
  5. 'target_line' => '001S TR'
  6. }
  7.  
Expand|Select|Wrap|Line Numbers
  1. But the Actually Hash is in this format:
  2. {
  3. 'line_number' => '21',
  4. 'filename' => 'BRANCH27',
  5. 'source_line' => {},
  6. 'target_line' => '001S TR'
  7. },
Apr 1 '08 #10

nithinpes
Expert 100+
P: 410
Actually after executing it is displaying this format -->

{
'line_number' => '21',
'filename' => 'BRANCH27',
'source_line' => {},
'target_line' => '001S TR'
},

But the Actually Hash is in this format:

{
filename' => 'BRANCH27',
line_number' => '21',
'source_line' => {},
'target_line' => '001S TR'
}
That's typically the behaviour of hash, the order won't be maintained. If you want to retain to retain the order, you can pass the order of keys into an array and use it while displaying.
Expand|Select|Wrap|Line Numbers
  1. @keys=('filename','line_number','source_line','target_line'); ## use the order of your choice
  2. foreach $k (keys %hash) {
  3. print "\n\n$k \n\n";
  4.  foreach $a (sort {$a->{'line_number'} <=> $b->{'line_number'}} @{$hash{$k}}) {
  5.   print "$_   : $a->{$_}\n" foreach(@keys); ## use @keys here
  6.   print "\n";
  7.    }
  8. }
  9.  
You would not have had this question if you had gone through Tie::SortHash that KevinADC mentioned in his reply.
Apr 1 '08 #11

P: 41
That's typically the behaviour of hash, the order won't be maintained. If you want to retain to retain the order, you can pass the order of keys into an array and use it while displaying.
Expand|Select|Wrap|Line Numbers
  1. @keys=('filename','line_number','source_line','target_line'); ## use the order of your choice
  2. foreach $k (keys %hash) {
  3. print "\n\n$k \n\n";
  4.  foreach $a (sort {$a->{'line_number'} <=> $b->{'line_number'}} @{$hash{$k}}) {
  5.   print "$_   : $a->{$_}\n" foreach(@keys); ## use @keys here
  6.   print "\n";
  7.    }
  8. }
  9.  
You would not have had this question if you had gone through Tie::SortHash that KevinADC mentioned in his reply.
Thank Nitin for ur help...
Apr 1 '08 #12

eWish
Expert 100+
P: 971
dillipkumar,

Four out of the the five posts that you have made in this tread you have neglected to use the code tags. Please use the code tags when posting code and data samples on this site.

[CODE]...your code goes here...[/CODE]

Thank You!

--Kevin
Apr 2 '08 #13

Post your reply

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