Hi all.
There is a simple task: parse a long string to readable format.
only two main rules:
1. string must be less than 15 chracters before \n
2. put \n after comma if it is possible
it can be done using cicle of course:
my $data = "0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,".('c'x50).",16,17,1
+8,19,20,21,22,23,24,25,26,27";
while ($data =~/[^\n]{16}/)
{
$data =~s/([^\n]{16})/ch($1)/e;
}
print $data;
sub ch
{
my $s = shift;
my $p = rindex($s, ',', 14)+1;
$p=15 unless($p);
return substr($s, 0, $p)."\n".substr($s, $p)
};
or with help of recursion function, but i want try to do it using executable regexp with g option (instead of while).
$data =~s/([^\n]{16})/ch($1)/ge;
but in with case result is wrong, because tail of previous result concatinates with head of new. To escape with situation we must to change current search position in regexp to earlier value (even if we set 0 it's ok).
Pos() function allow to change search position in regexp, but it seems it can't be use inside of regexp itself.
With variant is nothing changes
$data =~s/([^\n]{16})/pos($data)=0;ch($1)/ge;
So my question is: Is it possible change search position from regexp itself?
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.