sorting array of hash in hash | Member | | Join Date: Mar 2008
Posts: 32
| |
Hi,
I have a Hash: - %hash= 'student' => [
-
{
-
'roll_no' => 10,
-
'sub' => 'eng'
-
marks => 32,
-
},
-
{
-
'roll_no' => 11,
-
'sub' => 'math'
-
marks => 69,
-
},
-
{
-
'roll_no' => 10
-
'sub' => 'science'
-
marks => 69,
-
},
-
-
{
-
'roll_no' => 25
-
'sub' => 'geo'
-
marks => 59
-
},
-
-
],
I want to sort the hash according to sorting of 'roll_no' ..
After sorting It should be displayed - %hash= 'student' => [
-
{
-
'roll_no' => 10,
-
'sub' => 'eng'
-
marks => 32,
-
},
-
{
-
'roll_no' => 10
-
'sub' => 'science'
-
marks => 69,
-
},
-
-
{
-
'roll_no' => 11,
-
'sub' => 'math'
-
marks => 69,
-
},
-
-
{
-
'roll_no' => 25
-
'sub' => 'geo'
-
marks => 59
-
},
-
-
],
Can anybody help me...
|  | Expert | | Join Date: Jan 2007 Location: Southern California USA
Posts: 4,091
| | | re: sorting array of hash in hash
Is this school/class/course work?
|  | Moderator | | Join Date: Oct 2006 Location: New York, United States of America
Posts: 3,428
| | | re: sorting array of hash in hash
A hash has no order. If you want to have data sorted, use an array instead.
|  | Expert | | Join Date: Jan 2007 Location: Southern California USA
Posts: 4,091
| | | 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
| | | re: sorting array of hash in hash
Hi,
Actually this is a task..
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 '
-
},
-
],
-
-
);
i want to sort this hash..
| | Member | | Join Date: Mar 2008
Posts: 32
| | | 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: - %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 '
-
},
-
],
-
-
);
|  | Expert | | Join Date: Dec 2007
Posts: 400
| | | 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: -
foreach $k (keys %hash) {
-
print "\n\n$k \n\n";
-
foreach $a (sort {$a->{'line_number'} <=> $b->{'line_number'}} @{$hash{$k}}) {
-
print "$_ : $a->{$_}\n" foreach(keys %{$a});
-
print "\n";
-
}
-
}
-
| | Member | | Join Date: Mar 2008
Posts: 32
| | | 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: -
foreach $k (keys %hash) {
-
print "\n\n$k \n\n";
-
foreach $a (sort {$a->{'line_number'} <=> $b->{'line_number'}} @{$hash{$k}}) {
-
print "$_ : $a->{$_}\n" foreach(keys %{$a});
-
print "\n";
-
}
-
}
-
Hi Nitin,
Now it is working but the 'source_line' returns the hash like:
source_line : HASH(0x81ffdac)
|  | Expert | | Join Date: Dec 2007
Posts: 400
| | | 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
| | | 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 --> - {
-
filename' => 'BRANCH27',
-
line_number' => '21',
-
'source_line' => {},
-
'target_line' => '001S TR'
-
}
-
-
But the Actually Hash is in this format:
-
{
-
'line_number' => '21',
-
'filename' => 'BRANCH27',
-
'source_line' => {},
-
'target_line' => '001S TR'
-
},
|  | Expert | | Join Date: Dec 2007
Posts: 400
| | | 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. -
@keys=('filename','line_number','source_line','target_line'); ## use the order of your choice
-
foreach $k (keys %hash) {
-
print "\n\n$k \n\n";
-
foreach $a (sort {$a->{'line_number'} <=> $b->{'line_number'}} @{$hash{$k}}) {
-
print "$_ : $a->{$_}\n" foreach(@keys); ## use @keys here
-
print "\n";
-
}
-
}
-
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
| | | 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. -
@keys=('filename','line_number','source_line','target_line'); ## use the order of your choice
-
foreach $k (keys %hash) {
-
print "\n\n$k \n\n";
-
foreach $a (sort {$a->{'line_number'} <=> $b->{'line_number'}} @{$hash{$k}}) {
-
print "$_ : $a->{$_}\n" foreach(@keys); ## use @keys here
-
print "\n";
-
}
-
}
-
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...
|  | Moderator | | Join Date: Jul 2007 Location: Arkansas
Posts: 900
| | | 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
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,467 network members.
|