Okay, here we go again, except this time with the cartesian product (minus show duplicates), obtained with Math::Combinatorics::combine. Also, we had to set Data::Dumper::Deepcopy=1 to get a sane output from Data::Dumper.
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);
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|