in reply to Reducing HoH to AoA
Here's a vulgar-minded approach (no advanced algorithmics or smart intermediary data structures). Also I'm not sure if I understand correctly your idea of "linking" (though the code works for your examples):
for (keys %$start) { my @all = ($_, keys %{$start->{$_}}); my $processed; for my $setref (@$end) { my (@found, @not_found); for my $item (@all) { push @found, grep {$item eq $_} @$setref; } if (@found) { for my $item (@all) { push @not_found, $item unless grep {$item eq $_} @found; } push @$setref, @not_found if @not_found; $processed++; } } push @$end, [ @all ] unless $processed; }
Update: Please take into account that the code takes a few shortcuts. It makes no assumptions about the value in inner hashes (in your examples they're all "1"), and it doesn't handle well items linking to themselves (a => {a => 1, b => 1}) in some cases. These issues are easy to fix, though.
Update 2: Nope, it won't work, consider data below, and I don't think there's a solution that doesn't employ "advanced algorithmics" ;)
my $start = { a => {b => 1}, c => {d => 2}, d => {a => 1} };
|
|---|