#id layer/first layer/second id1 value1 value2 id2 value3 value4 #### #!/usr/bin/perl -w use strict; use Data::Dumper; my $list1 = "this/is/a/twig"; my $list2 = "this/is/a/branch"; my $list3 = "this/is/another/branch"; my $hash = {}; my $hashroot = $hash; my @hashtags1 = split(/\//, $list1); my @hashtags2 = split(/\//, $list2); my @hashtags3 = split(/\//, $list3); attach_leaf($hash,\@hashtags1, "leaf1"); attach_leaf($hash,\@hashtags2, "leaf2"); attach_leaf($hash,\@hashtags3, "leaf3"); print Dumper($hashroot); sub attach_leaf{ my ($tree, $branch, $leaf) = @_; # Work along the branch for(my $i = 0; $i < scalar(@$branch); $i++) { # build branch if($i != (scalar(@$branch) - 1)){ # build new branch if it doesn't exist already $tree->{$$branch[$i]} = {} if(! exists $tree->{$$branch[$i]}); $tree = $tree->{$$branch[$i]}; } # attach leaf else{ $tree->{$$branch[$i]} = $leaf; return $hash; } } } #### $VAR1 = { 'this' => { 'is' => { 'a' => { 'branch' => 'leaf2', 'twig' => 'leaf1' }, 'another' => { 'branch' => 'leaf3' } } } };