I have a string which comprised of 300 million bases;
$str = "ATCGTAGCTAGXCTAGCTAGCTGATXXXXATCGTAGCTAGCTGXTGCTAGCXXXXA...A";
I want to replace characters that are not [ATGC] in the string to something else, let's say to "A", meanwhile get the positions of characters that have be replaced;
I tried this:
while ($str=~/[^ATGC]/ig)
{
$pos = pos($str);
substr($str, $pos-1,1) = "A";
}
but the speed is not good.
Does anyone know better ways to do this?
Regexes can also substitute as well as match.
$str =~ s/X/A/g;
If you're only doing a single character, you can even use the tr operator.
$str =~ tr/X/A/g;
which may even be faster.
You can perform the replacement with regex directly using search and replace:
$str =~ s/X/A/ig;
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