Hi,
this time I don't need so much as a problem solved, as rather just an advice about a more elegant solution.
The scenario is:
I read a file which contains likes of the following pattern:
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!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.