http://www.perlmonks.org?node_id=772404


in reply to Re: Can we "grow" a string?
in thread Can we "grow" a string?

use strict; use warnings; use List::Util qw/min/; my $str = "ABCDEFGHIJKLMNOP"; my $seed = "FGH"; my $length = 7; my $r_lim = 2; my $l_lim = 1; my $slen = length $seed; my $ipos = index $str,$seed; my $lexpand = $slen < $length ? ($length - $slen)/2 : 0; $lexpand = min ($ipos, $l_lim, $lexpand); my $rexpand = $length - $slen - $lexpand; $rexpand = $r_lim < $rexpand ? $r_lim : $rexpand; print +(substr $str,($ipos - $lexpand),($lexpand + $slen + $rexpand)), +"\n";

citromatik

Replies are listed 'Best First'.
Re^3: Can we "grow" a string?
by Anonymous Monk on Jun 17, 2009 at 15:29 UTC
    Thank you very much for your time!
Re^3: Can we "grow" a string?
by Anonymous Monk on Jun 28, 2009 at 15:10 UTC
    Well, I have been dealing with this problem quite some days now, and it works OK...
    Except something I found out today:
    Both in the initial code and in this one, there is a bug and I don't know how to deal with it:
    Check for instance the initial code you provided citromatik:
    If the substring is NO, the final output is KLMNOP, that is, it doesn't take J as well so as to become 7, which is the wanted final length.