Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
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

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
Node Status?
node history
Node Type: note [id://968114]
help
Chatterbox?
[ambrus]: Like <c>{ package AnyEvent::Impl:: Prima; use Prima; sub io{ my($s,%r)=@_; Prima::File->new( file=>$r{fh},mask =>("w"eq$r{poll}? fe::Write():fe:: Read())|fe:: Exception,onRead =>$r{cb},onWrite =>$r{cb}, onException=>$r{cb }) } sub timer { ... } push @AnyEvent::REGI
[ambrus]: argh, too long, let me try on scratchpad
[Corion]: . o O ( I seem to have improved my skills of getting other people to write code for me )
[ambrus]: ambrus's scratchpad

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2016-12-08 12:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (141 votes). Check out past polls.