Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

sorting Array of hashes

by balajinagaraju (Sexton)
on Apr 03, 2012 at 08:10 UTC ( [id://963190]=perlquestion: print w/replies, xml ) Need Help??

balajinagaraju has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I am newbie to perl and i am stuck at some point where i wanted to sort a datastructure as shown below , its basically an array of hashes and i am clueless how to sort it. I need testcase1, 2 and 3 and so on in an order and i need the 'step' to be listed from step1 to stepN in increasing order and i also need to sort the values within each step if the parameters are similar to what is listed under step4, i need them in order from param1 to param4.. Can anyone help me in solving this.

my @array = ( 'testcase1' => { 'step2' => { 'command' => 'Goto' }, 'step1' => { 'param2' => 'dirpath', 'param1' => 'executablepath', 'command' => 'Launch ' }, 'step4' => { 'param4' => '580', 'param2' => '96', 'param3' => '726', 'param1' => '3', 'command' => 'CaptureRectanlge' }, 'step5' => { 'image1' => 'D:\\\\img1.jpg', 'image2' => 'D:\\\\img2.jpg ', 'command' => 'CompareImage' }, 'step3' => { 'param1' => '100', 'command' => 'Run' } }, 'testcase2' => { 'step2' => { 'command' => 'Goto' }, 'step1' => { 'param2' => 'dirpath', 'param1' => 'executablepath', 'command' => 'Launchsimulator ' }, 'step4' => { 'param4' => '580', 'param2' => '96', 'param3' => '726', 'param1' => '3', 'command' => 'CaptureRectanlge' }, 'step5' => { 'image1' => 'D:\\\\img1.jpg', 'image2' => 'D:\\\\img2.jpg ', 'command' => 'CompareImage' }, 'step3' => { 'param1' => '100', 'command' => 'Run' } }, 'testcase3' => { 'command6' => 'F', 'commandA' => 'J', 'command8' => 'H', 'command7' => 'G', 'command9' => 'I' }

Replies are listed 'Best First'.
Re: sorting Array of hashes
by Happy-the-monk (Canon) on Apr 03, 2012 at 08:48 UTC

    Why do you want to stort it?

    The hash just stores your data, when you have it printed out, that's just one particular form of representation.
    (An internal quirk if you will.)

    You may need sorting when you extract the data from the hash then.

    Your example looks a bit like Data::Dumper output. If my assumtion is right, setting $Data::Dumper::Sortkeys = 1; will probably do what you want.

    If it is more complicated, you will have to use the sort funtion.

    Cheers, Sören

      Hi, Thanks a lot for your suggestions , @soren adding this statement $Data::Dumper::Sortkeys = 1 in my program worked like charm...
Re: sorting Array of hashes
by nemesdani (Friar) on Apr 03, 2012 at 08:47 UTC
    If you'd like to sort the testcases as well, then use a hash instead of an array in the first place. And DON'T call it %hash :), it's just not healthy.
    Let @array be %testcases instead.
    You can then sort keys %testcases and process the embedded hashes as well.

    I'm too lazy to be proud of being impatient.
      Hi , Thanks for your input, i will incorporate your suggestions:-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://963190]
Approved by moritz
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-19 22:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found