I can use IxHash to remember the insertion order of a hash.

use Tie::IxHash;

my %hash;
tie(%hash, 'Tie::IxHash');
%hash = (
  x => 10,
  z => 20,
  q => { a1 => 1, a3 => 5, a2=>2,},
  y => 30,
);

printf("keys %s\n", join(" ", keys %hash));

=> keys x z q y

How about the nested hash?

printf("keys %s\n", join(" ", keys %{$hash{q}}));
keys a2 a1 a3

I suspect the answer is no as the q hash is anonymous and the order is lost before IxHash sees it.

I know that I can do Tie on $hash{q} and then add the elements, but I like using the single assignment to build the hash.

Is there a trick?

Edit
Report