I think I’ve just been looking at this too long. I have some data that looks like this:
@a = (
{ name => 'ethan', depth => 0 },
{ name => 'victoria', depth => 1 },
{ name => 'stephen', depth => 2 },
{ name => 'christopher', depth => 3 },
{ name => 'isabella', depth => 2 },
{ name => 'ethan', depth => 3 },
{ name => 'emma', depth => 0 },
{ name => 'michael', depth => 1 },
{ name => 'olivia', depth => 2 },
{ name => 'alexander', depth => 3 },
{ name => 'stephen', depth => 1 },
{ name => 'sophia', depth => 0 },
{ name => 'michael', depth => 1 },
{ name => 'ava', depth => 1 },
{ name => 'joshua', depth => 2 }
);
This is a simple ‘tree’ data structure. Every time ‘depth’ = 0, that is the beginning of a new ‘tree’. What I would like to know is in how many of these trees do each of the names appear? The desired result would be a single hash with the names as the key, and the count as the value.
The kink in this is, if you look closely, the first tree contains the name ‘ethan’ twice. Any tree can have any name more than once, but that should only count as 1, since they all occur in the same tree. However, ‘michael’ would have a count of 2, since he appears in two different trees.
I can think of a few ways of doing this, but they all involve multiple loops and seem somewhat brute force and inelegant. Hopefully, someone else out there can come up with a better solution.
I’m not 100% sure about your problem spec — is this the correct output?
If so, then this code seems to do the job:
that is, just keep a tally of names that only gets shuffled into the global count when we reach the root of the tree.