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

Re: Sort a hash table

Thread Previous
From:
Chris Ball
Date:
April 2, 2002 06:26
Subject:
Re: Sort a hash table
Message ID:
86hemu89du.fsf@void.printf.net
>>>>> "Michael" == Michael Stearman <qazwsxe7@hotmail.com> 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="printf.net"; 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


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About