【发布时间】:2022-04-18 11:02:23
【问题描述】:
有人可以帮我处理这个 Perl 代码吗?当我运行它时,什么也没有发生。没有错误或任何对我来说很奇怪的东西。它读入并打开文件就好了。我相信问题出在 while 循环或 foreach 循环中,因为老实说我不认为我理解它们。我对这方面很陌生,有一个很糟糕的老师。
说明:声明一个名为motif的标量变量并将其设为AAA。声明一个名为位置的数组变量,这是存储主题位置的位置。将基因放在一个标量变量中。现在在 amborella 基因中寻找那个基序。代码应打印图案的位置和找到的图案。您将需要编写一个 while 循环来搜索主题并包括 push、pos 和 –length 命令,以便保存和报告位置。然后,您将需要一个 foreach 循环来打印位置和主题。 (如果它只报告基因的第一行中的位置,请记住这是因为基因位于一个只会读取第一行的标量变量中。这是可以接受的。
到目前为止我的代码:
#!/usr/bin/perl
use warnings;
use strict;
#Declare a scalar variable called motif and make it AAA.
my$motif="AAA";
#Declare an array variable called locations, which is where the
#locations of the motif will be stored.
my@locations=();
my$foundMotif="";
my$position=();
#Place the gene in a scalar variable.
my$geneFileName = 'amborella.txt';
open(GENEFILE, $geneFileName) or die "Can't read file!";
my$gene = <GENEFILE>;
#Now search for that motif in the amborella gene.
#The code should print the position of the motif and the motif
#found. You will need to write a while loop that searches for the
#motif and includes push, pos, and –length commands in order to
#save and report locations.
while($foundMotif =~ m/AAA/g) {
$position=(pos($foundMotif)-3);
push (@locations, $position);
}
#Then you will need a foreach loop to print the locations and the motif.
foreach $position (@locations){
print "\n Found motif: ", $motif, "\n at position: ", $position;
}
#close the file
close GENEFILE;
exit;
【问题讨论】:
-
我们现在处于堆栈溢出和代码审查之间。我有一些关于你的风格的 cmets,这可能是因为教材过时了。有
strict和warnings很好。这些很有帮助。但是你也应该缩进你的代码,这样会更容易阅读。您的open调用存在一些问题,但它们并不是您的代码没有执行您想要的操作的原因(如果您有兴趣,请查看 3 argument open)。