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

  • Home
  • SEARCH
  • 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 9098761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:26:24+00:00 2026-06-17T00:26:24+00:00

I need to transform data structures from a list of arrays into a tree-like

  • 0

I need to transform data structures from a list of arrays into a tree-like one. I know the depth of the tree before I start processing the data, but I want to keep things flexible so I can re-use the code.

So I landed upon the idea of generating a subref on the fly (from within a Moose-based module) to go from array to tree. Like this (in a simplified way):

use Data::Dump qw/dump/;

sub create_tree_builder {
     my $depth = shift;
     return eval join '', 'sub { $_[0]->{$_[', 
                           join(']}->{$_[', (1..$depth)),
                          ']} = $_[',  $depth + 1 , '] }'; 
}


my $s = create_tree_builder(5);
my $tree = {};

$s->($tree, qw/one two three four five/, 'a value');

print dump $tree;

# prints
#  {
#     one => { two => { three => { four => { five => "a value" } } } },
#  }

This opened up worlds to me, and I’m finding cool uses for this process of eval-in a parametrically generated string into a function all over the place (clearly, a solution in search of problems).

However, it feels a little too good to be true, almost.

Any advice against this practice? Or suggestion for improvements?

I can see clearly that eval-ing arbitrary input might not be the safest thing, but what else?

Follow up

Thanks for all the answers. I used amon’s code and benchmarked a bit, like this:

use Benchmark qw(:all) ;

$\ = "\n";

sub create_tree_builder {
 my $depth = shift;
 return eval join '', 'sub { $_[0]->{$_[', 
               join(']}->{$_[', (1..$depth)),
              ']} = $_[',  $depth + 1 , '] }'; 
}


my $s = create_tree_builder(5);

$t = sub {
$_[0] //= {};

    my ($tree, @keys) = @_;
    my $value = pop @keys;

    $tree = $tree->{shift @keys} //= {} while @keys > 1;
    $tree->{$keys[0]} = $value;
};


cmpthese(900000, {
        'eval'  => sub { $s->($tree, qw/one two three four five/, 'a value') },
    'build' => sub { $t->($tree, qw/one two three four five/, 'a value') },

});

The results are clearly in favour of building the tree, not of the eval’ed factory:

            Rate build  eval
build  326087/s    --  -79%
eval  1525424/s  368%    -- 

I’ll admit I could have done that before. I’ll try with more random trees (rather than assigning the same element over and over) but I see no reason that the results should be different.

Thanks a lot for the help.

  • 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-06-17T00:26:25+00:00Added an answer on June 17, 2026 at 12:26 am

    It is very easy to write a generalized subroutine to build such a nested hash. It is much simpler that way than writing a factory that will produce such a subroutine for a specific number of hash levels.

    use strict;
    use warnings;
    
    sub tree_assign {
    
      # Create an empty tree if one was not given, using an alias to the original argument
      $_[0] //= {};
    
      my ($tree, @keys) = @_;
      my $value = pop @keys;
    
      $tree = $tree->{shift @keys} //= {} while @keys > 1;
      $tree->{$keys[0]} = $value;
    }
    
    tree_assign(my $tree, qw/one two three four five/, 'a value');
    
    use Data::Dump;
    dd $tree;
    

    output

    {
      one => { two => { three => { four => { five => "a value" } } } },
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to transform a list into dictionary as follows. The odd elements has
I need to transform input paramater string like 1-410000 54-420987 63-32000 into the structure
I'm getting data from the data layer that I need to transform in the
I am trying to copy data from an old database into a new one,
I need to transfer data from one table to another. The second table got
I need to transfer data daily from SQL Server (2008) to SQL Server (2005).
I need to transform my nested sets structure (mysql) into json for this spacetree
For an iPad application I need to transform some CoffeeScript files into JavaScript files
I need a way to transform numeric HTML entities into their plain-text character equivalent.
my xml DATA IS like:(This is xmlstring not an xmlfile and i need to

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.