perl 读取所需文件的路径,然后打开相应的文件

2019-10-01 12:36:51刘景俊

下面我们把修改过的代码写下:

#下面的程序是用来计算一段DNA序列中ATGC的数量的

#首先定义四种碱基的数量为0
$count_A=0;
$count_T=0;
$count_C=0;
$count_G=0;
#首先要先把序列进行合并成一行

#先确定所要处理的文件的路径及文件名(在windows系统下面要按照这样的例子写
#f:perldata.txt
print "please input the Path just like this f:perldata.txtn";
chomp($dna_filename=<STDIN>);
#打开文件
open(DNAFILENAME,$dna_filename)||die("can not open the file!");
#将文件赋予一个数组
@DNA=<DNAFILENAME>;

#以下两步要把所有的行合并成一行,然后去掉所有的空白符
$DNA=join('',@DNA);
$DNA=~s/s//g;


#然后依次读取字符串的元素,并对四种碱基的数量进行统计
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 "errorn"
 }
}
#输出最后的结果
print "A=$count_An";
print "T=$count_Tn";
print "C=$count_Cn";
print "G=$count_Gn";

得到的结果如下:

F:>perla.pl
please input the Path just like this f:perldata.txt
f:perldata.txt
error
A=40
T=17
C=27
G=24

F:>