monkprentice has asked for the wisdom of the Perl Monks concerning the following question:
2 [...] [line1] [line2] [line3] 3 [...] [line1] 1 [...] [line1] [line2] 2 [...] [line1] [line2]
The output code should be a simple reordering, while keeping the lineXs under each numbered indicator-line.
1 [...] [line1] [line2] 2 [...] [line1] [line2] [line3] 2 [...] [line1] [line2] 3 [...] [line1]
(The ordering of the "2 ..." lines does not matter).
I currently do it by maintaining as hash structure which maps {number-indicator => [indicator-line, sub-lines, indicator-line, sub-lines, ...]} And then write this hash structure back to the file.
The key problem is how to read one block of those lines (indicator line + all following lines until the next indicator or EOF). I do this in a loop but it looks quite clumsy that way. Is there an elegant way to use some sort of grep/map/random magic functions to do this ?
(I cannot even write an example of what I tried because I cannot think of anything good :D)
Consider this item as more of a puzzle.
Thanks!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Looking for elegance
by johngg (Canon) on Jan 27, 2014 at 10:41 UTC | |
|
Re: Looking for elegance
by hdb (Monsignor) on Jan 27, 2014 at 13:27 UTC | |
|
Re: Looking for elegance
by kcott (Archbishop) on Jan 28, 2014 at 00:14 UTC |