smarter ?? faster - for that you'll have to benchmark...
#!/usr/bin/perl
use strict; # https://perlmonks.org/?node_id=11138432
use warnings;
sub randomstring { join '', map +(qw(a b))[rand 2], 1 .. shift }
for (1 .. 10)
{
match( $_, randomstring(4), randomstring(4), 1);
}
sub match
{
my ($n, $x, $y, $m) = @_;
$x =~ /(.+)$(??{
($1 ^ substr $y, 0, length $1) =~ tr!\0!!c <= $m ? '' : '(*FAIL)'
})/ and print "$n:($-[1],@{[ length $1 ]})($x,$y)\n";
}
Outputs:
1:(1,3)(abba,baaa)
2:(3,1)(bbbb,aaba)
3:(1,3)(abba,abab)
4:(1,3)(abbb,bbaa)
5:(2,2)(baba,bbbb)
6:(1,3)(bbab,aabb)
7:(0,4)(aaaa,aaab)
8:(2,2)(baba,baab)
9:(2,2)(aaba,baaa)
10:(1,3)(bbab,bbba)
I hope I understood your problem correctly...
Update: fixed $_ to $n
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.