Reputation: 93
I have a hash:
use v6;
my %some-hash = a => 0,
b => 42,
c => 417;
and I'm trying to get its keys with %some-hash.keys
, which returns a Seq of all keys but not in the order in which they were declared. It seems that the order of keys is determined at hash initialization, because it changes if I run the code several times.
Is it possible to preserve the order (a b c)
?
P.S. %some-hash.keys.sort
will not suffice, since keys are expected to have arbitrary names.
Upvotes: 6
Views: 231
Reputation: 26924
Hash keys are randomly ordered, as you have seen.
There are modules in the ecosystem that could help you get what you want:
Or if these don't do exactly what you want, you can build your own Hash implementation with:
Upvotes: 9