> On Jul 21, 2015, at 12:40 PM, Simon Reinhardt <si...@keinstein.org> wrote:
> 
> Hi Team,
> 
> is there a ready solution to convert an linear array of hashrefs
> like this
> 
> [ {level => 0, value => "string1"},
>  {level => 1, value => "string2"},
>  {level => 2, value => "string3"},
>  {level => 2, value => "string4"}
> ]
> 
> into the nested data structure
> 
> [ {value => "string1",
>   kids => [
>       {value => "string2",
>        kids => [
>            {value => "string3"},
>            {value => "string4"}]}]}]
> 
> ? Any pointer appreciated.

That is an unique data structure and transformation requirement (as most are), 
so you are not likely to find an existing set of code to do exactly what you 
want. However, the transformation should not be too difficult, so you should 
try to code up something yourself and ask for help if you get stuck or would 
like to optimize your solution.

If I were coding this, I would like to know how complicated the data structure 
could become. For example, will there be multiple entries at levels 0 and 1, 
and how sould they be handled? 

The data structure you have shown is not very efficient in tems of data 
storage. Is this the actual data structure, or is it part of something more 
complicated. For exampe, a more efficient use of space would be this, which 
uses nested arrays instead of hashes:

[ 
 [0, “string1"],
 [1, “string2"],
 [2, “string3"],
 [2, “string4"]
]


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to