[LWN Logo]

From: Tom Christiansen <tchrist@mox.perl.com>
Subject: SRC: Insertion-order hashes
Date: 22 May 1998 13:40:03 GMT

Tired of your hashes not being in insertion order?
Use the CPAN Tie::IxHash module!

    # initialize
    use Tie::IxHash;

    tie %home, "Tie::IxHash";
    $home{Larry}    = "Mountain View";
    $home{Tom}      = "Boulder";
    $home{Malcolm}  = "Oxford";

    print "In insertion order, the people are:\n";
    foreach $name (keys %home) {
        print "\t$name\n";
    }

    $home{Gnat}  = "Fort Collins";

    print "\nStill in insertion order, the people's cities are:\n";
    while (( $name, $town ) = each %home ) {
        print "\t$name is in $town.\n";
    }

Here's the output:

    In insertion order, the people are:
	Larry
	Tom
	Malcolm

    Still in insertion order, the people's cities are:
	Larry is in Mountain View.
	Tom is in Boulder.
	Malcolm is in Oxford.
	Gnat is in Fort Collins.

--tom
-- 
Intel chips aren't defective.  They just seem that way.