in reply to Passing List::Compare a list of arrays
The easiest algorithm to find the common ancestor for @things is:
use List::MoreUtils qw(all); my ($first, @rest) = @things; my $ancestor = $first; while (defined $ancestor) { last if all { $_->has_ancestor($ancestor) } @rest; $ancestor = $ancestor->get_parent; }
This assumes that all the things have a get_parent method which returns the thing's parent, or undef if it has no parent; and has_ancestor which returns a boolean indicating if the first thing is descended (whether directly, or by many generations) from the second.
Obviously I've used OO in the above, but it's easy enough to adapt the algorithm to non-OO code.
|
|---|