I have the variables $pos
, $toRemove
and $line
. I would like to remove from this string $toRemove
from position $pos
.
$line = "Hello kitty how are you kitty kitty nice kitty";
$toRemove = "kitty";
$pos = 30; # the 3rd 'kitty'
I want to check if from position 30 there is string kitty
and I want to remove exactly this one.
Could you give me a solution of that? I can do it using a lot of loops and variables but it looks strange and works really slow.
if (substr($line, $pos, length($toRemove)) eq $toRemove) {
substr($line, $pos, length($toRemove)) = "";
}
$line = "Hello kitty how are you kitty kitty nice kitty";
$toRemove = "kitty";
$pos = 30; # the 3rd 'kitty'
pos($line) = $pos;
$line =~ s/\G$toRemove//gc;
print $line;
output:
Hello kitty how are you kitty nice kitty
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With