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

I think I know how to handle this case, but I just want to
Think about a table like this ID Value 100 1 100 3 101 1
I think the file that is produced is an .asm file, any idea how
I think most people know how to do this via the GUI (right click
I think this is specific to IE 6.0 but... In JavaScript I add a
I think that java executables (jar files) are trivial to decompile and get the
I think this is a multi-part question, so bear with me. Currently all of
Think of a small and basic affiliate system. I want an URL like www.myshop.com/mynewproduct.html?afid=123
Think that says it all?
Think about a speedometer, and imagine all these little strokes around it like 10

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.