第一种方式,我们运用的for循环,然后用了if......else.......结构
for ($position=0;$position<length $DNA;++$position)
{
$base=substr($DNA,$position,1);
if ($base eq 'A')
{
$count_A=$count_A+1;
}
elsif ($base eq 'T')
{
$count_T=$count_T+1;
}
elsif ($base eq 'C')
{
$count_C=$count_C+1;
}
elsif ($base eq 'G')
{
$count_G=$count_G+1;
}
else
{
print "error\n"
}
}
第二种方法,我们运用while循环:
#然后依次读取字符串的元素,并对四种碱基的数量进行统计
while ($DNA=~/a/ig){$count_A++}
while ($DNA=~/c/ig){$count_C++}
while ($DNA=~/g/ig){$count_G++}
while ($DNA=~/T/ig){$count_T++}
while ($DNA=~/[^acgt]/ig){$e++}
得到的结果如下:
F:\>perl\a.pl
please input the Path just like this f:\\perl\\data.txt
f:\\perl\\data.txt
A=40
T=17
C=27
G=24
F:\>
第三种方法,我们使用tr///的结构来进行碱基的计数:
$count_A=($DNA=~tr/Aa//);
$count_T=($DNA=~tr/Tt//);
$count_G=($DNA=~tr/Gg//);
$count_C=($DNA=~tr/Cc//);
结果如下:
F:\>perl\a.pl
please input the Path just like this f:\\perl\\data.txt
f:\\perl\\data.txt
A=40
T=17
C=27
G=24
F:\>
这三种方法依次更简单,并且效率越来越高!