Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Section Handling - Nested

by hdb (Monsignor)
on Jun 21, 2013 at 05:44 UTC ( [id://1040076]=note: print w/replies, xml ) Need Help??


in reply to Section Handling - Nested

In your output, where is the last "data data data data" line coming from? In the input there are 7 such lines, in the output, there are 8!

This is a really nice question and can be very elegantly solved using an array as a stack.

Replies are listed 'Best First'.
Re^2: Section Handling - Nested
by hdb (Monsignor) on Jun 21, 2013 at 13:02 UTC

    After I claimed earlier today, that the problem has an elegant solution based on a stack, I have to publish my proposal here, despite the fact that there is no sign that veera has spend any effort on solving this.

    use strict; use warnings; my @stack; while(<DATA>) { if( s|^(<(\d+)>.*)(</\2>\n)|$1\n| ) { print pop @stack while @stack and substr( $stack[-1], 2, -2 ) >= $ +2; push @stack, $3; } print; } print reverse @stack; __DATA__ <1>one</1> data data data data <2>two</2> data data data data <3>three</3> data data data data <3>blah</3> data data data data <2>blah</2> data data data data <4>zoom</4> data data data data <4>zoom</4> data data data data <1>data</1> data data data data

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1040076]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-18 00:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found