Ironically, perhaps Tie::IxHash , which was motivated by the desire to get the hash keys in that order, is as close as you are going to get to what you want.
In the Tie::IxHash keys and values are stored in array references. keys returns a copy of the key set, but something like (tied %hash)->[1] will give you direct access to it.
Removing elements in Tie::IxHash is O (n). A possible workaround for this would be to replace the undef values rather than deleting them. That is, preferring
$ixhash{$obsolete_key} = undef;
to
delete $ixhash{$obsolete_key};
Or, if you could combine your deletions - if you could organize your code so that you would usually call delete several keys at the same time and between other hash operations - then there is room for improvement on Tie::IxHash .
source share