One possibility is to do what you sometimes do with arrays: specify the keys.
for (0..$#a) { # Sorted array keys say $a[$_]; } for (sort keys %h) { # Sorted hash keys say $h{$_}; } for (0, 1, 3) { # Sorted array keys say $h{$_}; } for (qw( Key1 Key2 Key3 )) { # Sorted hash keys say $h{$_}; }
You can also get ordered values โโas follows:
my @values = @h{qw( Key1 Key2 Key3 )};
source share