develooper Front page | perl.beginners | Postings from April 2002

Re: Sort a hash table

Thread Previous
Chris Ball
April 2, 2002 06:26
Re: Sort a hash table
Message ID:
>>>>> "Michael" == Michael Stearman <> writes:

    Michael> Does anyone know how to alphabetically sort the keys in a
    Michael> hash table?  Thanks,

Hi, Michael.

You can find the answer to this question in 'perldoc', under the FAQ
section.  The command to run would be 'perldoc -q 'sort a hash''.

The first section is reprinted below, hope this helps.

- Chris.

Found in /usr/share/perl/5.6.1/pod/perlfaq4.pod
       How do I sort a hash (optionally by value instead of key)?

       Internally, hashes are stored in a way that prevents you
       from imposing an order on key-value pairs.  Instead, you
       have to sort a list of the keys or values:

           @keys = sort keys %hash;    # sorted by key
$a=""; Chris Ball | chris@void.$a | www.$a | finger: chris@$a
As to luck, there's the old miners' proverb: Gold is where you find it.

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About