in reply to Can we "grow" a string?
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Can we "grow" a string?
by Anonymous Monk on Jun 17, 2009 at 12:44 UTC | |
by citromatik (Curate) on Jun 17, 2009 at 13:23 UTC | |
by Corion (Patriarch) on Jun 17, 2009 at 12:48 UTC | |
by Anonymous Monk on Jun 17, 2009 at 13:10 UTC |