![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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;
Last edited by cbkihong; 05-06-2005 at 05:50 AM. Reason: Additional Information |
|
#3
|
|||
|
|||
|
one more problem
one more thing ... wat if i want to put 'ques' 32 and 'ques' 18 together and other 'ques' sorted ...
|
|
#4
|
|||
|
|||
|
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. |
|||
| Google The UNIX and Linux Forums |