use strict; use warnings; use Data::Dumper; use Math::Combinatorics qw(combine); my $HoA_show_characters = { flintstones => [ "fred", "barney" ], jetsons => [ "george", "jane"], }; # create all possible combis along the lines of: #[ # { "flinstones" => "fred" }, # { "flintsones" => "barney" } #]... etc my $AoH_show_characters = []; foreach my $show (keys %$HoA_show_characters) { foreach my $character ( @{ $HoA_show_characters->{$show} } ) { push @$AoH_show_characters, ( { $show => $character } ); } } #now make all possible combis of 2 from the above aoH. # but only accept combis from different shows my $AoH_show_characters_two_at_a_time; foreach my $a_of_hash_combis( combine( 2, @$AoH_show_characters ) ) { my $key1 = ( keys %{ $a_of_hash_combis->[0] } )[0]; my $key2 = ( keys %{ $a_of_hash_combis->[1] } )[0]; unless ( $key1 eq $key2) { #key 1 equals key 2 push @$AoH_show_characters_two_at_a_time, ( $a_of_hash_combis ); } } $Data::Dumper::Deepcopy = 1; print Dumper($AoH_show_characters_two_at_a_time);