473,387 Members | 1,465 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,387 software developers and data experts.

sorting array of hash in hash

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
12 2211
KevinADC
4,059 Expert 2GB
Is this school/class/course work?
Mar 31 '08 #2
Ganon11
3,652 Expert 2GB
A hash has no order. If you want to have data sorted, use an array instead.
Mar 31 '08 #3
KevinADC
4,059 Expert 2GB
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
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
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
410 Expert 256MB
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
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
410 Expert 256MB
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
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
410 Expert 256MB
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
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
971 Expert 512MB
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

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

Similar topics

13
by: dr. zoidberg | last post by:
Hello, I have mysql table: +----+---------+ | 1 | value1 | +----+---------+ | 2 | value2 | +----+---------+ | 3 | value3 |
2
by: Me | last post by:
I have a hash (of a hashes) that has data like this table1->table_name ->table_size table2->table_name ->table_size
3
by: Tommo | last post by:
Hello All, whilst I know how to sort an array I am having problems with my situation, what I would ideally like is something like a 'sort keys' action with Perl hashes. I am reading in data and...
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
8
by: guitarromantic | last post by:
Hey, I have a 'staff' table with a column 'status', corresponding to a 'statuses' table with the appropriate titles (eg 1 | Editor in Chief). I want to display on the page the headings (the...
4
by: john sutor | last post by:
Does anyone know how to sort a hash table?
6
by: Fred Morrison | last post by:
Do you know of a way to load a hash table with random key/value pairs (e.g., 2/"Two",1/"One",3/"Three") and then iterate through the entries in "sorted" (key sequence) order...
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...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.