[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] subexpr nearest the 'middle' of expr



> sub replace_mid
> {my($s,$t,$r)=@_;my$l=length$s;1while$l--
> &&$s!~s/(.{$l,})$t(.{$l})/$1$r$2/s;$s;}

I count 78 characters -- much better than my 110

sub replace_mid{($e,$s,$r)=@_;$l=length$e;$m=($l-length$s)/2;for
$i(0..$l/2){substr($e,$m-$i)=~s/^((..){$i})?\Q$s/$r/s&&last}$e}

Mine does strings rather than regexes, but it's not strict-safe.
If I modify yours to compare apples with apples (and get rid of the 
extra semicolon) and use $_, yours beats mine by even more -- it's 
down to 72:

sub replace_mid
{($_,$t,$r)=@_;$l=length;1while$l--
&&!s/(.{$l,})\Q$t\E(.{$l})/$1$r$2/s;$_}

To do regexes, kill the \Q and \E, and it's down to 68.

-- 
Keith C. Ivey <kcivey@cpcug.org>
Washington, DC

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe