Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Converting HoA into AoH

by tphyahoo (Vicar)
on Oct 31, 2005 at 14:37 UTC ( #504271=note: print w/replies, xml ) Need Help??

in reply to Converting HoA into AoH

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);

Replies are listed 'Best First'.
Re^2: Converting HoA into AoH
by neversaint (Deacon) on Oct 31, 2005 at 15:19 UTC
    Dear tpyahoo,
    Thanks for the answer. But why the results has to be this complex/deep? I just want simple AoH.

    neversaint and everlastingly indebted.......
      I'm not really clear on what you mean by that. Is what you had in readmore tags the result that you want? This skips over the women, so I'm assuming that's not it.

      Is it the code you are calling complex, or the resulting datastructure? What exactly is the final datastructure you are looking for?

      Do you want to take your show / char hash combos two at a time, or is one at a time okay? (In which case you get an array of eight hashes.) If an array of eight hashes is what you want, try dumping $AoH_show_characters after the second chunk of code.

      Otherwise, if you want four arrays of two hashes each, like my code produces, I can't see any way to get the job done without this kind of maybe slightly confusing nesting.

      Good luck!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://504271]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2020-06-06 21:49 GMT
Find Nodes?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?

    Results (41 votes). Check out past polls.