Connecting Tech Pros Worldwide Forums | Help | Site Map

sorting array of hash in hash

Member
 
Join Date: Mar 2008
Posts: 32
#1: Mar 31 '08
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...

KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#2: Mar 31 '08

re: sorting array of hash in hash


Is this school/class/course work?
Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#3: Mar 31 '08

re: sorting array of hash in hash


A hash has no order. If you want to have data sorted, use an array instead.
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#4: Mar 31 '08

re: sorting array of hash in hash


Quote:

Originally Posted by Ganon11

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.
Member
 
Join Date: Mar 2008
Posts: 32
#5: Apr 1 '08

re: sorting array of hash in hash


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..
Member
 
Join Date: Mar 2008
Posts: 32
#6: Apr 1 '08

re: sorting array of hash in hash


Quote:

Originally Posted by Ganon11

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.              );
nithinpes's Avatar
Expert
 
Join Date: Dec 2007
Posts: 400
#7: Apr 1 '08

re: sorting array of hash in hash


Quote:

Originally Posted by dillipkumar

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.  
Member
 
Join Date: Mar 2008
Posts: 32
#8: Apr 1 '08

re: sorting array of hash in hash


Quote:

Originally Posted by nithinpes

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)
nithinpes's Avatar
Expert
 
Join Date: Dec 2007
Posts: 400
#9: Apr 1 '08

re: sorting array of hash in hash


Quote:

Originally Posted by dillipkumar

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.
Member
 
Join Date: Mar 2008
Posts: 32
#10: Apr 1 '08

re: sorting array of hash in hash


Quote:

Originally Posted by nithinpes

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. },
nithinpes's Avatar
Expert
 
Join Date: Dec 2007
Posts: 400
#11: Apr 1 '08

re: sorting array of hash in hash


Quote:

Originally Posted by dillipkumar

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.
Member
 
Join Date: Mar 2008
Posts: 32
#12: Apr 1 '08

re: sorting array of hash in hash


Quote:

Originally Posted by nithinpes

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...
eWish's Avatar
Moderator
 
Join Date: Jul 2007
Location: Arkansas
Posts: 900
#13: Apr 2 '08

re: sorting array of hash in hash


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
Reply