A simple way to accomplish this could be to use index in combination with substr:
use strict; use warnings; my $str = "ABCDEFGHIJKLMNOP"; my $seed = "FGHI"; my $length = 7; my $slen = length $seed; my $ipos = index $str,$seed; my $lexpand = $slen < $length ? ($length - $slen)/2 : 0; $lexpand = $ipos < $lexpand ? $ipos : $lexpand; print +(substr $str,($ipos - $lexpand),$length),"\n";
This outputs DEFGHIJ as expected. The problem raises when the "expanded" substring reaches the beginning or the end of the reference string.
Update: Solved the limit cases:
"ABCDEFGHIJKLMNOP" "ABC" 7 => ABCDEFG "ABCDEFGHIJKLMNOP" "BC" 7 => ABCDEFG "ABCDEFGHIJKLMNOP" "MN" 7 => JKLMNOP
citromatik
In reply to Re: Can we "grow" a string?
by citromatik
in thread Can we "grow" a string?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |