Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 955655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:24:59+00:00 2026-05-16T00:24:59+00:00

I think I’ve just been looking at this too long. I have some data

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:25:00+00:00Added an answer on May 16, 2026 at 12:25 am

    I’m not 100% sure about your problem spec — is this the correct output?

      alexander 1
            ava 1
    christopher 1
           emma 1
          ethan 1
       isabella 1
         joshua 1
        michael 2
         olivia 1
         sophia 1
        stephen 2
       victoria 1
    

    If so, then this code seems to do the job:

    my @names = (
      # your @a above
    );
    
    my (%seen, %count);
    
    for my $entry (@names) {
      if ($entry->{depth} == 0) {
        ++$count{$_} for keys %seen;
        %seen = ();
      }
      ++$seen{ $entry->{name} };
    }
    
    ++$count{$_} for keys %seen;
    print "$_\t$count{$_}\n" for sort keys %count;
    

    that is, just keep a tally of names that only gets shuffled into the global count when we reach the root of the tree.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Think I have an integer array like this: a[0]=60; a[1]=321; a[2]=5; now I want
I think I have a basic understanding of this, but am hoping that someone
Think that I have many activities,and all I want is this: I have a
Think I have domain.com that redirects into some.otherdomain.com . I don't want to show
I think I have a pretty simple problem that is just pretty difficult to
I think this could be a very easy question for you. But I have
I think that asking this might be kind of silly but I'm still wondering
I think this has probably been asked, but after reading a lot, I'm not
Think I have an activity with some layouts, I want to know is it
Think at this scenario: I have a c# windows form application. This application was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.