in reply to How might I handle a special tree?

The target structure is an array of arrays, see perldsc

You can use splice to extract regions out if the original array (i.e. if you don't want to copy to a new one)

Otherwise just slice and copy.

 $new[$i++] =  [ @old[$start..$end ] ]

edit

And for traversing use a recursive function which calls itself when an array-ref is read.

Cheers Rolf

(addicted to the Perl Programming Language and ☆☆☆☆ :)

Replies are listed 'Best First'.
Re^2: How might I handle a special tree?
by vitoco (Hermit) on Dec 07, 2014 at 22:57 UTC

    I thought about AoA, but that is not enought. For example:

    [1,2,3,4,5,6,7,8,9]

    should be converted to something like this:

    [1,[2,3,4],5,[6,[7,8,9]]]

    What is missing in this structure is the placeholder, which will have a group name... that's a new value in the list!

    I guess that I could manage the first element in each list as the name of the placeholder, resulting something like this:

    [A,1,[B,2,3,4],5,[C,6,[D,7,8,9]]]

    BTW, while traversing, how could I recognize if current element is a scalar or another list to recurse?

      > how could I recognize if current element is a scalar or another list to recurse?

      with ref

      > What is missing in this structure is theplaceholder, which will have a group name... 

      No idea what you may mean...

      update

      Maybe

       [ {A => [1, {B => [2,3,4]} ,5, {C => [ 6, { D => [7,8,9] } ] } ]

      ? ? ?

      (untested)

      update

      TIMTOWTDI

      Instead of a one element hash you could use a 2 element array.

      Or put the "placeholder" into the sub array as first element.

      I personally prefer the hash solution for readability, but keep in mind that keys are stringified.

      Cheers Rolf

      (addicted to the Perl Programming Language and ☆☆☆☆ :)

        Thanks for ref. I missed that ;-)

        Your latest structure example looks nice, but I think it would complicate the search and replace algorithms, more than what BrowserUk said.

        I think I'll stay with my AoA proposal, with the first element as the group name (adding a null or "root" element to the original list).