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

Hi: I would like to know the correct way to do the following: I am trying to sort an array of hashes. I create the array of hashes with the entry $char[$pos]{$z}++; where the $pos range is from 1..1000 and $z values is from 1..200. Each time I found $z I increment the FREQUENCY of this. So how I can print this 1000 array sorting this hash by FREQUENCY? So if I want to see the frequency of 10 in the positions 5 I write $char[5]{10}. If I want to see all $z in order of frequency in the position 5, how can I sort this array of hash?

Replies are listed 'Best First'.
Re: Sorting a array of hashes
by GrandFather (Saint) on Nov 13, 2007 at 06:07 UTC

    Making wild guesses about what you may actually be trying to achieve and almost entirely ignoring the minuscule amount of code that you have provided, the following something like what you are after:

    use strict; use warnings; my @paras = ( {a => 10, b => 3, e => 15, k => 1}, {a => 8, d => 4, e => 17, r => 5}, ); my $paraNum = 1; for my $para (@paras) { my @letters = sort {$b->[0] <=> $a->[0]} map {[$para->{$_}, $_]} keys %$para; print "Paragraph $paraNum:\n"; print join ("\n", map {"$_->[1]: $_->[0]"} @letters), "\n"; ++$paraNum; }

    Prints:

    Paragraph 1: e: 15 a: 10 b: 3 k: 1 Paragraph 2: e: 17 a: 8 r: 5 d: 4

    Perl is environmentally friendly - it saves trees
      Thank GrandFather, this work very good.