tobold.org

correct • elegant • free

△ comp.lang.perl △

◅ Converting from base 8 to base 2

Need to fork and exec and pipe! ▻

Sorting Associative Arrays

In article <31167e8a.44540000@news.imaginet.fr>,
Derek Erb (Paris) <erb@erb.com> wrote:
>This is probably an easy one... but I'm obviously going about it
>wrong.

It's either easy or impossible, depending on your point of view. :-)

>I want to sort an associative array, in reverse order, by its keys.

You can't.  Associative arrays (aka hashes) are unordered.

What you probably want is something like this...

    foreach $key (reverse sort keys %hash) {
            # Do your thang, for example...
            print "$key => $hash{$key}\n";
    }

Reading from the inside out, `keys %hash' returns a (*not* associative)
array containing all the keys of the hash.  `reverse sort' first
sorts, then reverses this array.  Finally, the `foreach' iterates
over each element of it.

Tim.
--
Tim Goodwin   | "Forget the mystical level numbers." -- Henry Spencer

Original headers:

From: tim@pipex.net (Tim Goodwin)
Newsgroups: comp.lang.perl.misc,comp.infosystems.www.authoring.cgi
Subject: Re: Sorting Associative Arrays
Date: 6 Feb 1996 14:51:28 GMT
Organization: Unipalm PIPEX
Message-ID: <4f7ptg$ln9@wave.news.pipex.net>
References: <31167e8a.44540000@news.imaginet.fr>

△ comp.lang.perl △

◅ Converting from base 8 to base 2

Need to fork and exec and pipe! ▻