Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

sort in array of hash required

by upaksh (Novice)
on Nov 16, 2011 at 12:43 UTC ( [id://938364]=perlquestion: print w/replies, xml ) Need Help??

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

hello monks, I need help with following code:

@Aname = [ { 200 => 'hello', name => 'akshay' }, { 100 => 'world', name => 'india' }, { 150 => 'great', name => 'ahm' } ]

i need to sort this so that I get the keys in order of 100, 150 and 200. Thanks

Replies are listed 'Best First'.
Re: sort in array of hash required
by Corion (Patriarch) on Nov 16, 2011 at 12:48 UTC
Re: sort in array of hash required
by raybies (Chaplain) on Nov 16, 2011 at 13:39 UTC

    I would reorganize your array of hashes, such that the numbers were hash elements with a fixed keyname,that would make the sorting simpler, I think. Otherwise you have to sort the keys of each each hash in each array element. And because you have multiple keys, some of which are words, you'd only want to sort the keys that you were able to pass off as numbers... which would require some kind of funky stuff that makes me cringe. :)

    What if you organized your hash like such?

    @Aname=( { keynum => 200, keyname => 'hello', name =>'akshay' }, { keynum => 100, keyname => 'world', name =>'india' }, { keynum => 150, keyname => 'great', name =>'ahm' }, );
    Then, to sort @Aname by the keynum field, you'd only need to do this:
    my @sortedAname = sort {$a->{keynum} <=> $b->{keynum}} (@Aname);

    Anyhoo... that's what I'd do to simplify the sorting...

    --Ray

    PS. Also you don't want to assign @Aname with [], that means you're assigning An array of one element, which is a reference to an array. You want to assign the @array using () (list of actual values, not a reference to one). (Corrected in my code above...)

      To print out the data:
      print "#keynum, keyname, name\n"; print "$_->{'keynum'}, $_->{'keyname'}, $_->{'name'}\n" for @sortedAna +me;
      How could I optimize the repetitive $_->{'<key>'} structure?

      Have a nice day, j

        Use a hash slice.

        knoppix@Microknoppix:~$ perl -E ' > @arr = ( > { > keynum => 50, > keyname => q{fruit}, > name => q{plum}, > }, > { > keynum => 100, > keyname => q{fish}, > name => q{cod}, > }, > { > keynum => 200, > keyname => q{fowl}, > name => q{duck}, > }, > ); > printf qq{%10s%10s%10s\n}, qw{ #keynum keyname name }; > printf qq{%10d%10s%10s\n}, @$_{ qw{ keynum keyname name } } > for @arr;' #keynum keyname name 50 fruit plum 100 fish cod 200 fowl duck knoppix@Microknoppix:~$

        I hope this is helpful.

        Cheers,

        JohnGG

        How could I optimize ...

        Note that johngg's approach of Re^3: sort in array of hash required is not an 'optimization' in the sense of execution speed; I doubt it would run significantly, if at all, faster.

        However, it offers, IMHO, potential significant advantages in terms of maintainability. E.g., the group of keys  keynum keyname name can be factored out (if you could get rid of that pesky '#' that's buzzing around in there) into an array definition (possibly made a constant) and used in many places without concern for having 'forgotten something'. If it becomes necessary to change anything about this group of keys, it is only necessary to do so in one place.

Re: sort in array of hash required
by GrandFather (Saint) on Nov 16, 2011 at 19:52 UTC
    @Aname = map {$_->[0]} sort {$a->[1] <=> $b->[1]} map { [$_, sort {$a <=> $b} grep {/^\d+$/} keys %$_] } @Aname;

    This will sort by the smallest integer key in each @Aname element. If there will only ever be a single key you can omit the sort in front of the grep.

    True laziness is hard work
      /me starts cringing... ;)

      (This is exactly what I was referring to (see my above reply)... :-)

      I bow to your amazing PerlMonk Brain, Grandfather.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2024-04-25 15:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found