使用tr ///运算符计算字符串中的字母 [英] Using tr/// operator to count letters in a string
本文介绍了使用tr ///运算符计算字符串中的字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在序列或字符串中计算A的数量,C和G的数量。我写了以下代码。
I would like count number of A's, C's and G's in a sequence or string. I have written the following code.
但是当我打印的值,只有A的打印。 C和G显示为零。
在下面的代码中,我在评估A的第一个,但如果我通过评估C的第一次切换顺序,我得到C的值,但现在A和G的打印为零。
But when I print the values, only A's get printed. C's and G's are displayed as zero. In the code below I'm evaluating A's first, but if I switch the order by evaluating C's first, I get the values of C, but now A's and G's are printed out as zero.
任何人都可以告诉我我的代码有什么问题?谢谢!
Can anyone tell me what is wrong with my code? Thanks!
#! /usr/bin/perl
use strict;
use warnings;
open(IN, "200BP_junctions_fasta.faa") or die "Cannot open the file: $!\n";
while(<IN>)
next if $_ =~ /\>/;
my $a = ($_ = tr/A//);
my $c = ($_ = tr/C//);
my $g = ($_ = tr/G//);
print "A:$a, C:$c, G:$g\n";
}
文件如下所示:
> A_Seq
ATGCTAGCTAGCTAGCTAGTC
> B_Seq
ATGCGATCGATCGATCGATAG
推荐答案
将 $ _ = tr /
更改为 $ _ =〜tr /
。此外,您缺少了和
的开放括号。
Change your $_ = tr/
to $_ =~ tr/
. Also, you're missing an open brace for your while
.
这篇关于使用tr ///运算符计算字符串中的字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文