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 5975053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:01:46+00:00 2026-05-22T21:01:46+00:00

I have an array of arrays of names that could be represented in Ruby

  • 0

I have an array of arrays of names that could be represented in Ruby like this:

samples = [
  %w[a],
  %w[a c],
  %w[a],
  %w[a],
  %w[b],
  %w[b],
  %w[a],
  %w[a e],
  %w[a e],
  %w[a c d],
  %w[a c d],
  %w[b],
  %w[b c e],
  %w[b c e],
  %w[a c],
  %w[a e],
  %w[a e]
]

These are the output of a sampling profiler, where each list of names represents the call stack for a particular sample. I want to display these as a top-down tree of named-values where the value at each node is the sum of hits to that particular call path.

For the above sample input, the output tree should be:

root:0
  a:4
    e:4
    c:2
      d:2
  b:3
    c:0
      e:2

(I don’t want an ASCII output as shown above, but rather a tree structure that represents this.)

What is simple, efficient code that produces this output?
I have my own solution which I will post as an answer, but which seems to me less than ideal.

Edit: I forgot to include the fact that the tree should be sorted in descending value at each level. I’ve added sample nodes and changed the output to reflect this.

  • 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-22T21:01:47+00:00Added an answer on May 22, 2026 at 9:01 pm

    [edit] A recursive Tree class with functional programming (I use ostruct for the sake of simplicity):

    require 'ostruct'
    
    class Tree < OpenStruct 
      def self.new_from_array(plain)
        Tree.new(:node => "root", :count => 0, :children => children_from_array(plain))
      end
    
      def self.children_from_array(plain)
        plain.group_by(&:first).map do |node, group|
          terminal, leaves = group.map { |xs| xs.drop(1) }.partition(&:empty?)
          Tree.new(:node => node, :count => terminal.size, :children => children_from_array(leaves))
        end.sort_by(&:count).reverse    
      end
    
      def inspect(indent=0)
        node_info = " "*indent + "#{self.node}: #{self.count}" 
        ([node_info] + self.children.map { |tree| tree.inspect(indent+2) }).join("\n")
      end  
    end
    

    Example:

    >> Tree.new_from_array(samples)
    =>
    root: 0
      a: 4
        e: 4
        c: 2
          d: 2
      b: 3
        c: 0
          e: 2
    

    You can customize inspect to fit your visualization needs.

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

Sidebar

Related Questions

I have an array named 'names' with strings looking like this: [name_23_something, name_25_something, name_2_something];
I have two arrays, @names and @employees , that are filled with strings that
I have a array of names and I want to use these names for
I have an array with lots of items with same names like CloudObserverCMSStub edited
I have an array of div names, the div of which I would like
i have a file that looks like this: TTITLE0=track name 1 TTITLE1=track name 2
I have a column chart in Highcharts that looks roughly like this: | |
I have an array in PHP that is simply strings. These strings represent relationships
I'd like to have an array of arrays in a class using Zend and
I have an array that has keys and values. For eg: Array ( [name]

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.