Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: sort in array of hash required

by raybies (Chaplain)
on Nov 16, 2011 at 13:39 UTC ( #938375=note: print w/replies, xml ) Need Help??

in reply to sort in array of hash required

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...


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...)

Replies are listed 'Best First'.
Re^2: sort in array of hash required
by reisinge (Friar) on Nov 16, 2011 at 15:53 UTC
    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.



      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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://938375]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2018-04-26 02:52 GMT
Find Nodes?
    Voting Booth?