Say I'd like to move the first char in a string to the other end. An obvious choice might be
s/(.)(.+)/$2$1/
But I wondered- what if I tried to move it pairwise along the string (bubble it). It seems like as long as I could get the matching position to move by 1, instead of 2, I could do it that way. Just for academic reasons I wondered..
It seems like something along these lines, with the positive lookhead, could potentially bubble the char:
s/(.)(?=.)/$2$1/g;
which I'd hoped would perform these steps:
12345
21345
23145
23415
23451
but instead it produced 5 warnings and left the string unchanged (I presume the warnings were that $2 was undef)..
Thoughts, wondrous monks?
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.