I just wrote a bit of code to compare 2 'trees', where a tree is a hashref of hashrefs or hashrefs ...
The code is general in that it will deal with arbitrary depth trees, but it seems like i did more work than i needed to, so im looking for better solutions.
The comparison is following every path down 1 tree, and seeing if that path exists in the other tree. If not, report the entire path that wasnt in the second tree. The path is reported as an arrayref of the keys down the path. So the function returns an arrayref of the path arrayrefs that are in the first tree but not the second.
Anyway, heres the code, any comments would be appreciated:

Update:
modified the return in the else of expand_trees_to_arrays()
Because i realized i had 1 thing wrong in the problem specification, i.e. the values in the nodes are irrelevant to what i am looking for, so i dont want any reporting done there, i only want to compare the paths down to the nodes. Sorry for the change
sub compare_hashtrees { my ($tree_1, $tree_2) = @_; my @tree_1_only; foreach my $tree_1_key (keys %$tree_1) { my $sub_arrays_not_in_tree2 = []; if ( ! exists $tree_2->{$tree_1_key} ) { # the subtree is missing from tree_2 - expand it $sub_arrays_not_in_tree2 = expand_tree_to_arrays($tree_1->{$tree_1_key}); } elsif ( ref $tree_1->{$tree_1_key} ) { # another level of hashing $sub_arrays_not_in_tree2 = compare_hashtrees($tree_1->{$tree_1_key}, $tree_ +2->{$tree_1_key}); } foreach my $sub_array (@$sub_arrays_not_in_tree2) { push @tree_1_only, [$tree_1_key, @$sub_array]; } } return \@tree_1_only; } sub expand_tree_to_arrays { my ($tree) = @_; if ( ref $tree ) { my @expanded; foreach my $key (sort keys %$tree) { my $sub_expanded = expand_tree_to_arrays($tree->{$key}); foreach my $path (@$sub_expanded) { push @expanded, [$key, @$path]; } } return \@expanded; } else { # return [ [$tree] ]; # its a scalar return [ [ ] ]; } }

In reply to Tree comparisons by shemp

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.