Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

How can I make my hash remember the order I put elements into it?

by faq_monk (Initiate)
on Oct 08, 1999 at 00:20 UTC ( #633=perlfaq nodetype: print w/replies, xml ) Need Help??

Current Perl documentation can be found at perldoc.perl.org.

Here is our local, out-dated (pre-5.6) version:

Use the Tie::IxHash from CPAN.

    use Tie::IxHash;
    tie(%myhash, Tie::IxHash);
    for ($i=0; $i<20; $i++) {
        $myhash{$i} = 2*$i;
    }
    @keys = keys %myhash;
    # @keys = (0,1,2,3,...)

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
choroba's discovered two instances of a weird animal in the code at work: \@$var
[Eily]: hum, tied scalar, generates a new array when derefed ? :D
[Corion]: choroba: Ah, when doing a shallow copy, I prefer [ @$var ] - I think \@$var might return the same reference instead of a reference to a fresh copy
[Corion]: perl -wle "my $ar = [1,2,3]; my $br=\@$ar; print qq($ar / $br)" confirms this to me
[karlgoethebier]: "discovered" this to sum an array: perl -E 'say scalar map {1 .. $_} (1..3);'

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2017-09-26 08:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (293 votes). Check out past polls.

    Notices?