Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Return values from a key sorted hash

by SavannahLion (Pilgrim)
on Sep 10, 2010 at 21:36 UTC ( [id://859700]=perlquestion: print w/replies, xml ) Need Help??

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

I'm pretty sure I spotted this snippet somewhere but now I can't recall where or how this is done.
Is there a more elegant way to obtain a key ordered list or array hash values.
In other words,
A) Order the hash based on keys
B) return a list of the values based on their key order.

#unrelated code here my @data; for (sort keys %ABC) { push(@data, $ABC{$_}); } return @data;

The number of values is expected to be small (six values)

Any ideas on this?

Replies are listed 'Best First'.
Re: Return values from a key sorted hash
by BrowserUk (Patriarch) on Sep 10, 2010 at 22:11 UTC

    my %hash = ...; my @keyOrderedValues = @hash{ sort keys %hash };

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Return values from a key sorted hash
by Your Mother (Archbishop) on Sep 10, 2010 at 21:48 UTC
    my @data = map { $abc{$_} } sort keys %abc; # :)

    Update, as a sub-

    # Takes a hash ref, returns an array ref. sub key_sorted_values { my $hash_ref = shift; # Validate if needed. [ map { $hash_ref->{$_} } sort keys %{$hash_ref} ]; }
Re: Return values from a key sorted hash
by roboticus (Chancellor) on Sep 10, 2010 at 22:15 UTC

    SavannahLion:

    It's just like this:

    #!/usr/bin/perl use strict; use warnings; my %tmp = ( a=>1, b=>2, c=>3 ); my @vals = @tmp{reverse sort keys %tmp}; print "@vals\n";

    Which prints (on my system):

    Roboticus@Roboticus-PC ~ $ perl t.pl 3 2 1

    All the magic is in the single line:

    my @vals = @tmp{reverse sort keys %tmp};

    Basically, you're just returning the list of values from a hash slice:

    • reverse sort keys %tmp builds our sorted list of keys, and
    • @tmp{ ... } tells perl to build the list of values for the selected keys.

    ...roboticus

      Why reverse? (And it ain;t "magic"!)

        BrowserUk:

        No real reason, just a demonstration. I just put in reverse for fun, intending to illustrate that you could put in any order.

        ...roboticus

Re: Return values from a key sorted hash
by dasgar (Priest) on Sep 10, 2010 at 21:53 UTC

    If you're wanting to keep the keys in the same order that they were created, you might want to check out Tie::IxHash.

    If you're not really concerned about maintaining the order, there is an example in perlfaq4 about sorting a hash.

Re: Return values from a key sorted hash
by SavannahLion (Pilgrim) on Sep 11, 2010 at 00:14 UTC

    Thanks bunches for the replies. The samples you guys (or gals) gave were just the sort of thing I was seeking.

      fyi, it's called a hash slice

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://859700]
Approved by AnomalousMonk
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-20 16:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found