use strict; use warnings; my %hash = ( one => 1, two => 2, three => 3, four => 4, five => 5 ); my $priority_key = 'three'; sub prioritize { my( $href, $find ) = @_; my @prioritized_keys; while( my( $key, $value ) = each %{$href} ) { if( $key ne $find ) { push @prioritized_keys, $key; } else { unshift @prioritized_keys, $key; } } return \@prioritized_keys; } my @keyset = @{ prioritize( \%hash, $priority_key ) }; print "@keyset\n"; #### use strict; use warnings; my %hash = ( one => 1, two => 2, three => 3, four => 4, five => 5 ); my $priority_key = 'three'; sub prioritize { my( $href, $find ) = @_; my @keyring = keys %{$href}; foreach my $idx ( 0 .. $#keyring ) { next unless $keyring[ $idx ] eq $find; unshift( @keyring, splice( @keyring, $idx, 1 ) ); last; } return \@keyring; } my @keyset = @{ prioritize( \%hash, $priority_key ) }; print "@keyset\n";