Well, I am quite scatter-brained at the moment. Under more ideal circumstances, I would "sleep on this problem" and solve it in the morning -- but I have to get this part done ASAP so I can continue with the larger task at hand.
As such I keep tearing down my past efforts -- but just so you know I am working on this instead of just waiting for others to do my work for me ...
sub recurse {
my ($ref, $car, @cdr) = @_;
if ($prev_car eq $car) {
my $hash = { name => $car, children => [] };
push @$ref, $hash;
return unless @cdr;
$prev_car = $car;
recurse($hash->{children}, @cdr);
}
push @$ref, { name => $car, children => [] };
return unless @cdr;
$prev_car = $car;
recurse($ref, @cdr);
}
Which produces the following output:
I forgot to say "Thanks in advance" too. :) |