My solution is not really a competitor, since I'm not that
familiar with Perl Golf customs, so bear with me.
use strict;
sub f {
my ($a,$b,$m,$n) = @_;
my (@r) = ();
my $l = " $a! $b";
while($l =~ s/(.*?)(( \w+){$m,$n} )(.*!.*\2.*)/ $1 =$4/) {
push @r, $2;
};
@r
};
Why I'm posting this at all is, that a previous version
exhibited unanticipated (by me, that is) behaviour :
# Previous version :
my $l = " $a! $b";
while($l =~ s/(.*)(( \w+){$m,$n} )(.*!.*\2.*)/ $1 =$4/) {
push @r, $2;
};
@r
That version only found the $m-word
matches, because the greedy (.*) at the beginning
forced the RE engine to work its way backwards through
the string. As soon as I sacrificed another character,
this problem went away. I haven't given the golfing much
thought (at work, I can only think about concepts, not
byte-fiddle with REs without raising suspicions ... ).
-
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.