Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Retrieving the hash key-value in the order of their insertion without Tie::IxHash

by johngg (Abbot)
on Apr 30, 2012 at 17:38 UTC ( #968114=note: print w/ replies, xml ) Need Help??


in reply to Retrieving the hash key-value in the order of their insertion without Tie::IxHash

One way is to use a HoH and keep the insertion order alongside the data.

knoppix@Microknoppix:~$ perl -Mstrict -wE ' > my @keys = qw{ pete bill mary jack dave leah }; > my $order = 0; > my %hash; > > foreach my $key ( @keys ) > { > $hash{ $key } = { data => $order * 2, order => ++ $order }; > } > > say qq{$_ => $hash{ $_ }->{ data } } > for sort { $hash{ $a }->{ order } <=> $hash{ $b }->{ order } } > keys %hash;' pete => 0 bill => 2 mary => 4 jack => 6 dave => 8 leah => 10 knoppix@Microknoppix:~$

I hope this is helpful.

Cheers,

JohnGG


Comment on Re: Retrieving the hash key-value in the order of their insertion without Tie::IxHash
Download Code
Re^2: Retrieving the hash key-value in the order of their insertion without Tie::IxHash
by temporal (Pilgrim) on May 01, 2012 at 13:56 UTC

    That's fine for a bulk insert, but if he wanted to keep manipulating the hash it gets pretty cumbersome to upkeep.

    Strange things are afoot at the Circle-K.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://968114]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (10)
As of 2015-07-06 21:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (83 votes), past polls