wfsp has asked for the wisdom of the Perl Monks concerning the following question:
into;p;one ;greybox_start; ;h2;two ;greybox_end; ;p;three
The code produces$VAR1 = [ ['div',{'id' => 'article'}, ['p','one'], ['div',{'class' => 'greybox'}, ['h2','two'], ], ['p','three'] ] ];
(whitespace trimmed)$VAR1 = [ ['div',{'id' => 'article'}, ['p','one'], ['div',{'class' => 'greybox'}], ['h2','two'], ['p','three'] ] ];
Clearly, my attempt at keeping track of $depth isn't working
Any ideas on how this might be put right (or take a different approach altogether)?
#!/usr/local/bin/perl use strict; use warnings; use Data::Dumper; $Data::Dumper::Indent = 1; my @lines = <DATA>; chomp @lines; my @list = ([q{div}, {id => q{article}}]); my (@current_list); my $depth = 0; for my $line (@lines){ my ($tag, $txt) = $line =~ /^;([^;]+);(.*)/; if ($tag eq q{greybox_start}){ push @{$list[$depth]}, @current_list; push @{$list[$depth]}, [q{div}, {class => q{greybox}}]; $depth++; @current_list = (); next; } elsif ($tag eq q{greybox_end}){ $depth--; push @{$list[$depth]}, @current_list; @current_list = (); next; } push @current_list, [$tag, $txt]; } push @{$list[$depth]}, @current_list; print Dumper(\@list), __DATA__ ;p;one ;greybox_start; ;h2;two ;greybox_end; ;p;three
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Generating a list of lists (for HTML::Element) from trivial markup
by jethro (Monsignor) on Jul 27, 2008 at 13:18 UTC | |
by RMGir (Prior) on Jul 27, 2008 at 14:00 UTC | |
|
Re: Generating a list of lists (for HTML::Element) from trivial markup
by RMGir (Prior) on Jul 27, 2008 at 13:05 UTC | |
|
Re: Generating a list of lists (for HTML::Element) from trivial markup
by alexm (Chaplain) on Jul 27, 2008 at 23:25 UTC |