If you can't rely on having only one tag per line, then I'd probably go with something like this:
my @list= "external"; s[(?<=<)([^>]+)(?=>))]{ my $tag= $1; if( $tag eq "item" ) { "$list[-1]-$tag"; } else { if( $tag eq "end-list" ) { pop @list if 1 < @list; } elsif( $tag =~ /(\w+)-list/ ) { push @list, $1; } $tag; } }ge;
Note that you can still read the input line-by-line to avoid having to fit some entire huge file into memory at once (or set local $/= ">";); just put my first line outside of your read/write loop and the s///ge inside the read/write loop.
- tye
In reply to Re: Nested list items (s///ge)
by tye
in thread Nested list items
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |