UPDATE: modified to deal with jumps larger than 1 back up the hierarchy.

Very nice puzzle. Here is my proposal: (it does not do the { inlist => [... bits, but they can be inserted where indicated)

use strict; use warnings; use Data::Dumper; my %type = ( '*' => 'u', '#' => 'o' ); sub type { $type{ substr shift, -1 } } my @lines = map { /([*#]*)(\d*)\s+(.*)/; $2?[$1,[$3,{value=>$2}]]:[$1, + $3] } <DATA>; my @lists = ( [] ); my $lasttype = ''; my $lastitem = $lists[-1]; my @stack; for (@lines) { my( $type, $item ) = @$_; if( not $type ) { push @lists, []; $lastitem = $lists[-1]; $lasttype = ''; @stack = (); next; } if( $type eq $lasttype ) { push @$lastitem, $item; next; } if( not $lasttype ) { push @$lastitem, type($type), [ $item ]; $lastitem = $lastitem->[-1]; $lasttype = $type; push @stack, [ $lastitem, $lasttype ]; next; } if( length( $type ) > length( $lasttype ) ) { # modify this block fo +r { inlist => [... push @stack, [ $lastitem, $lasttype ]; $lastitem->[-1] = [ $lastitem->[-1], [ type($type), [$item] ] ]; $lastitem = $lastitem->[-1]->[1]->[1]; $lasttype = $type; next; } for( 1..(length( $lasttype ) - length( $type )) ) { ( $lastitem, $lasttype ) = @{ pop @stack }; } push @$lastitem, $item; } print Dumper \@lists; __DATA__ * list 1 unordered item 1 * list 1 unordered item 2 *# list 1 unordered item 2 ordered item 1 *# list 1 unordered item 2 ordered item 2 *# list 1 unordered item 2 ordered item 3 * list 1 unordered item 3 ** list 1 unordered item unordered item 1 ** list 1 unordered item unordered item 2 ** list 1 unordered item unordered item 3 **# list 1 unordered item unordered item 3 ordered item 1 **# list 1 unordered item unordered item 3 ordered item 2 **# list 1 unordered item unordered item 3 ordered item 3 * list 1 unordered item 4 # list 2 ordered item 1 #3 list 2 ordered item 2 # list 2 ordered item 3 #* list 2 ordered item 3 unordered item 1 #* list 2 ordered item 3 unordered item 2 #* list 2 ordered item 3 unordered item 3 # list 2 ordered item 4

In reply to Re: Need help with filling a complicated data structure by hdb
in thread Need help with filling a complicated data structure by Lady_Aleena

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.