The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
perl hashes question lucho_1 Shell Programming and Scripting 1 04-08-2008 11:06 PM
How to declare hashes in KSH similar to Perl ? tipsy Shell Programming and Scripting 2 08-03-2006 11:24 AM
perl: hashes, whiles, and last effigy Shell Programming and Scripting 0 02-10-2005 09:23 PM
sort() array of strings in perl photon Shell Programming and Scripting 2 10-19-2004 10:27 AM
CGI passing arrays/hashes to another CGI script WIntellect Shell Programming and Scripting 2 01-13-2003 08:22 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 05-06-2005
Registered User
 

Join Date: May 2005
Posts: 6
How To Sort Array of Hashes ??

Some one plz help me how to sort an array of hashes .....

for e.g i have an array as

@AoH = (
{
ques => 10,
marks => 32,
},
{
ques => 32,
marks => 22,
},
{
ques => 2,
marks => 41,
},
);

now i want to sort this array with increasing value of "ques" ..... plz help

Last edited by coolguyshail; 05-06-2005 at 02:46 AM. Reason: question not clear
Reply With Quote
Forum Sponsor
  #2  
Old 05-06-2005
Moderator
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,477
If this is Perl, this is an array of hash references. You can't strictly make an array of hashes in Perl, as this will eventually become a single array.

It's actually very simple:

Code:
@sorted = sort { $$a{'ques'} <=> $$b{'ques'} } @AoH;
EDIT: Actually the perldsc manpage has all the details (hopefully it won't overwhelm you!)

Last edited by cbkihong; 05-06-2005 at 05:50 AM. Reason: Additional Information
Reply With Quote
  #3  
Old 05-08-2005
Registered User
 

Join Date: May 2005
Posts: 6
one more problem

one more thing ... wat if i want to put 'ques' 32 and 'ques' 18 together and other 'ques' sorted ...
Reply With Quote
  #4  
Old 05-09-2005
Moderator
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,477
Perl's sort function is immensely powerful (and somewhat difficult to explain) that lets you sort in any way you desire. Theoretically you can put the condition (the comparator) in the sort {} block to generate a custom ordering for your case. But your question is not precise. And you cannot simply say keep 32 and 18 together, because what should be the ordering of other array items with respect to 32 and 18, respectively? And the order of 32 and 18? If these questions are not defined, the condition in the sort {} block will not be accurate to establish a total ordering, and the sorted results will then be non-deterministic (i.e. simply wrong).

Mathematically speaking, given a total of n items to sort, you need to define all possible permutations of any two items in the block, i.e. n * n, and the ordering should be absolutely consistent. So, if you define a certain item x(i) -> 32 and 32 -> 18, the condition in the block should never, ever, generate the order 18 -> x(i), or any other items preceding x(i).

In general, if a given sort order is not intuitive, and you cannot specify an algorithm to establish the total ordering, I will suggest getting the normally sorted results and manipulate the rest yourself with another array. For this, I guess you should try to do this yourself.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 04:54 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0