in reply to Specify Output of Keys in Hash

Are you using a hash because you want efficient lookups of individual keys? Because you want to assure uniqueness of keys? Or simply because you want to express key=>value relationships?

If your primary interests are key/value relationships and insert order, you may not need a hash so much as a List of Lists, as in:

my @array = ( [ 'snake' => 1 ], [ 'dog ' => 2 ], [ 'zebra' => 3 ], ); print "$_->[0], " for @array;

Hashes by nature are not "ordered" in any meaningful way. If you need to preserve the O(1) lookups of hash elements, while also preserving some sort of order, you could keep track of key insertions in one array, and the key/values in a hash. Essentially the array is an ordered index of hash keys that way.

The tie approach creates tie overhead for every access. That may not be much of an issue, but it is there.

The LoL approach described at the top of my post assumes that you don't care about O(1) lookups at all, and just care about relationships and order.

Different approaches depending on what you really need to do. ....what do you need to do, anyway?


Dave