in reply to Write code using less lines

Another way, also without modification of the original string, perhaps slightly faster:

>perl -wMstrict -le "my $str = 'ABC---DR----EEEEEGGGG-GRE-RED----KKKK---'; print qq{'$str'}; my $len = $str =~ tr/A-Za-z//; print qq{'$str'}; print $len; " 'ABC---DR----EEEEEGGGG-GRE-RED----KKKK---' 'ABC---DR----EEEEEGGGG-GRE-RED----KKKK---' 24

Replies are listed 'Best First'.
Re^2: Write code using less lines
by johngg (Canon) on Apr 19, 2010 at 18:54 UTC

    You can save a little typing and also be a little more comprehensive with respect to "anything not a dash" by using the c flag (complement) of tr, see Quote and Quote like Operators.

    knoppix@Microknoppix:~$ perl -E ' > $str = q{ABC---DR----EEEEEGGGG-GRE-RED----KKKK---}; > $len_seq = $str =~ tr{-}{}c; > say $len_seq;' 24 knoppix@Microknoppix:~$

    I hope this is of interest.

    Cheers,

    JohnGG