Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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

by johngg (Canon)
on Apr 30, 2012 at 17:38 UTC ( [id://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

Replies are listed 'Best First'.
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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://968114]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-03-28 16:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found