in reply to Quick way to find the letter with the most occurences in a string

With a problem like this where you know the 3 possibilities, tr is usually the way to go for speed. Splitting and making hashes and such will usually be slower than 3 very fast trips through the string. "save me a number of lines" would normally be the last thing I would be thinking about, ranking way below: clarity, speed, memory usage. Its hard to argue with clearly written fast code.
#!/usr/bin/perl -w use strict; my $string="CCCAAAAACCCAAAAAAACCCCBBBCCAAACCBBACBBAAAAAAAAAAACAAAAAAAA +AA"; my $nA = $string =~ tr/A//; my $nB = $string =~ tr/B//; my $nC = $string =~ tr/C//; print "A=$nA B=$nB C=$nC"; #A=37 B=7 C=16 #compare these 3 as you will to handle cases like #all are the same, etc
  • Comment on Re: Quick way to find the letter with the most occurences in a string
  • Download Code