my @hash_keys = qw/foo bar baz quux w x y z/; my %foo; my %bar; for my $key (@hash_keys) { $foo{$key} = 1; print "$key: ", join(':', keys %foo), "\n"; } for my $key (reverse @hash_keys) { $bar{$key} = 1; print "$key: ", join(':', keys %bar), "\n"; }