If you just want to count characters, then tr or //g are likely the fastest. tr doesn't nicely interpolate, so I'm just looking at //g, and put it in list context:
local $_ = $string; my $total; for my $letter (qw(A C G T)) { my $count =()= /$letter/g; print "$letter: $count\n"; $total += $count; } my $other = length($_) - $total; print "Other: $other\n";
Depending on the length of $string a direct assignment might be faster, but you should Benchmark that with your data. Using code in a regex replacement causes various things to get set up and torn down again, so the overhead of passing over the string four times needs to be weighed against the overhead of a subroutine/block call in the regex:
local $_ = $string; local %count; s/([ACGT])/$count{$1}++;$1/ge;
In reply to Re: Iterating over string
by Corion
in thread Iterating over string
by danielfortin86
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |