Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
how can I sort marks in descending order across the departments and institutes?my %hash=(inst => { dept1 => { rollno=> { [name,marks], [name1,marks1], }, inst2.... });
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: sorting in hashes
by tmharish (Friar) on Aug 22, 2009 at 14:03 UTC | |
A - rather long, and inefficient solution - but one that is readable and hopefully instructive is below:
| [reply] [d/l] [select] |
Re: sorting in hashes
by james2vegas (Chaplain) on Aug 22, 2009 at 13:24 UTC | |
do you want to sort by inst, dept, marks? I think we need a little more description of your structure, rollno is that a constant, or is it a value like isnt or dept1? why are the marks in array refs inside a hashref, which wouldn't work if you had an odd number of names. You would want them either in an arrayref like rollno => [ [name, marks], ... ] or in a hashref like rollno => { name => marks, name => marks, ... }. And as mentioned in another response, supply what your data would look like sorted. | [reply] [d/l] [select] |
by Util (Priest) on Aug 22, 2009 at 13:39 UTC | |
| [reply] [d/l] [select] |
Re: sorting in hashes
by Util (Priest) on Aug 22, 2009 at 16:32 UTC | |
Output:
| [reply] [d/l] [select] |
Re: sorting in hashes
by targetsmart (Curate) on Aug 22, 2009 at 13:38 UTC | |
Vivek -- 'I' am not the body, 'I' am the 'soul', which has no beginning or no end, no attachment or no aversion, nothing to attain or lose. | [reply] |
Re: sorting in hashes
by zwon (Abbot) on Aug 22, 2009 at 13:41 UTC | |
It's not very clean from you description what structure do you have (there are more '{' than '}') and how exactly do you want to sort it, so can you provide us this more thorough description? Generally it looks like you need to sort array of hashes, perhaps several times. Have a look onto sort, there's example of sorting array of hashes. | [reply] |
Back to
Seekers of Perl Wisdom