I don't know exactly if this is the type of solution
you and
BrowserUK were looking for (working towards), but it seems to work.. and should cut back on the rescanning thing. I left the
(?{print pos($_)."\n"}) (it can be taken out if it is to much of an eyesore), to show where it starts matching, and uncommenting the
use re 'debug'; line seems to confirm this. Anyhow here is the code:
use strict;
use warnings;
#use re 'debug';
$_ = "17341234173412341734123417341234";
my $i;
pos() = $i
while s<\G
(?{print pos($_) . "\n"})
(.*?)(.)(...)\2
(?{$i=pos()-4})
>
<defined $1?$1."A".$3."B":"A".$3."B">ex;
print;
__END__
0
1
3
4
9
11
12
17
19
20
25
27
28
A7AAB2BBA7AAB2BBA7AAB2BBA7AAB2BB
update:
FWIW I did do much benchmarking using cmpthese in the
benchmark module, and it appears that after all the hand waving, and tweaks, nothing seems to run faster than
BrowserUK's substr solution, and probably the one I would go with for long strings of digits. But I would make one last change tweak first. That being changing the first
. to a
\d .
That is:
substr($a, $_, 5) =~ s[(\d)(...)\1][A$2B] for 0 .. length ($a);
Which should speed up the work the regex engine has to do as it will skip all the positions that do not start with a digit (and hence have already been turned into a letter (: )
For that matter the original could have been made better doing the same:
s!(\d)(...)\1!A$2B!;
-enlil
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.