Dear monks,
I am a Perl newbie and I would like to kindly ask for the help of experts to write a code snippet for my research project in order to parse some text files.
The task:
I have some ranges like the following:
5-10,14-18,22-27
and a string that I have transformed into a series of characters like so:
1-2-3-4-5-6-7-8-...30
(not all strings are of the same length of course)
Now, what I need to do for my analysis is two things:
1. substitute the numbers that belong to the ranges with a character, say X. So, instead of
5-6-7-8-9-10 for instance, I will have
X-X-X-X-X-X
This I have managed to do
2. Starting with a specific character, say A from the beginning (in this case 1), substitute the remaining numbers, interchangably with A and B, when they are separated by a group of X's. For instance, I would do A-A-A-A (instead of 1-2-3-4) until I find 5 where the X's begin and then B-B-B (instead of 11-12-13) and then A-A-A instead of 19-20-21 and, finally B-B-B instead of 28-29-30.
The initial string is then:
1-2-3-4-5-6-7-8-9-10-11-12-13-14-15-16-17-18-19-20-21-22-23-24-25-26-2
+7-28-29-30
and the expected one is:
A-A-A-A-X-X-X-X-X-X-B-B-B-X-X-X-X-X-A-A-A-X-X-X-X-X-X-B-B-B
Can you please help me with the second point that I cannot solve?
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.