Dear Monks,
I have a great challenge for you to show your regexp and/or other perl skills. I am working on a text file parser, which is importing records into a database. Each row is a document which have a chapter list identifier, like 1.1. or 2.4.5.6
When i am reading a row, i want to know the prior chapter to the current one. For example 1.1.'s prior is 1., 2.4.5.6's is 2.4.5.
So the question is if how you dig it out from the identifier. I have a solution, (so it is not a homework that i want to be solved by others), but I would like to see your faster, more clever, and more obfuscated solutions! So chapter up!
My solution:
if ($chapter=~/([\d+\.]+)\d+\.?/) {
print "$1 is prior to $chapter";
}
As you see it is not sure if the point is presented at the end of the chapter identifier or not ( \.? )
--
tune
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.