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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |