#!/usr/bin/perl # http://perlmonks.org/?node_id=1191725 use strict; use warnings; my %hash1; $hash1{'1'} = 1; $hash1{'2'} = 1; $hash1{'3'} = 1; $hash1{'4'} = 1; $hash1{'5'} = 1; my %hash2; $hash2{'1'}{key} = 'a'; $hash2{'2'}{key} = 'b'; $hash2{'3'}{key} = 'a'; $hash2{'4'}{key} = 'a'; $hash2{'5'}{key} = 'b'; print map $_->[2], sort { $a->[1] cmp $b->[1] or $a->[0] <=> $b->[0] } map [ $_, $hash2{$_}{'key'}, "key $_ and $hash2{$_}{'key'}\n" ], keys %hash1;