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

I have this code.
#!/usr/bin/perl use strict; my %hash = ( 'snake' => 1, 'dog' => '2', 'zebra' => '3' ); print join ',', keys %hash;
The output is:
snake,dog,zebra

But I want it to be:
dog,zebra,snake
So, I would like to be able to specify the output in the keys of the hash.

Replies are listed 'Best First'.
Re: Specify Output of Keys in Hash
by davido (Cardinal) on Jul 13, 2011 at 20:57 UTC

    Are you using a hash because you want efficient lookups of individual keys? Because you want to assure uniqueness of keys? Or simply because you want to express key=>value relationships?

    If your primary interests are key/value relationships and insert order, you may not need a hash so much as a List of Lists, as in:

    my @array = ( [ 'snake' => 1 ], [ 'dog ' => 2 ], [ 'zebra' => 3 ], ); print "$_->[0], " for @array;

    Hashes by nature are not "ordered" in any meaningful way. If you need to preserve the O(1) lookups of hash elements, while also preserving some sort of order, you could keep track of key insertions in one array, and the key/values in a hash. Essentially the array is an ordered index of hash keys that way.

    The tie approach creates tie overhead for every access. That may not be much of an issue, but it is there.

    The LoL approach described at the top of my post assumes that you don't care about O(1) lookups at all, and just care about relationships and order.

    Different approaches depending on what you really need to do. ....what do you need to do, anyway?


    Dave

Re: Specify Output of Keys in Hash
by AnomalousMonk (Archbishop) on Jul 13, 2011 at 20:09 UTC

    Maybe something like this:

    >perl -wMstrict -le "my %hash = ( snake => { value => 1, order => 3, }, dog => { value => 2, order => 1, }, zebra => { value => 3, order => 2, }, ); my $out = join q{,}, sort { $hash{$a}{order} <=> $hash{$b}{order} } keys %hash ; ;; print qq{'$out'}; " 'dog,zebra,snake'
Re: Specify Output of Keys in Hash
by i5513 (Pilgrim) on Jul 13, 2011 at 20:08 UTC
      Or Tie::Hash::Indexed: compatible module, but written in XS (C), and not in pure Perl, and thus: faster.
Re: Specify Output of Keys in Hash
by bart (Canon) on Jul 14, 2011 at 11:37 UTC
    Your wish is very non-obvious to me: the keys in the desired output are not in alphabetical order, not in order of increasing or decreasing value, and not in insertion order.

    So... What is your rule to determine the desired sort order? If it's not insertion order, then you might decide to just sort the keys. See the FAQ: How do I sort a hash (optionally by value instead of key)?