gpt4 book ai didi

perl - 如何在具有不同标题的文件中搜索字符串?

转载 作者:行者123 更新时间:2023-12-05 01:09:42 26 4
gpt4 key购买 nike

我正在使用 perl 在不同标题下列出不同序列的文件中搜索特定字符串。当存在一个序列(即一个标题)但我无法推断它时,我能够编写脚本。假设我需要在给定文件中搜索一些字符串“FSFSD”然后例如:无法搜索文件是否包含以下内容:

Polons
CACAGTGCTACGATCGATCGATDDASD
HCAYCHAYCHAYCAYCSDHADASDSADASD
Seliems
FJDSKLFJSLKFJKASFJLAKJDSADAK
DASDNJASDKJASDJDSDJHAJDASDASDASDSAD
Teerag
DFAKJASKDJASKDJADJLLKJ
SADSKADJALKDJSKJDLJKLK

当文件只有一个标题时可以搜索,即:

Terrans
FDKFJSKFJKSAFJALKFJLLJ
DKDJKASJDKSADJALKJLJKL
DJKSAFDHAKJFHAFHFJHAJJ

我需要将结果输出为“在标题 abc 下找到的字符串 xyz

我使用的代码是:

print "Input the file name \n";
$protein= <STDIN>;
chomp $protein;
unless (open (protein, $protein))
{
print "cant open file \n\n";
exit;
}
@prot= <protein>;
close protein;
$newprotein=join("",@prot);
$protein=~s/\s//g;
do{
print "enter the motif to be searched \n";
$motif= <STDIN>;
chomp $motif;
if ($protein =~ /motif/)
{
print "found motif \n\n";
}
else{
print "not found \n\n";
}
}
until ($motif=~/^\s*$/);
exit;

最佳答案

看到你的代码,不回答你的问题,我想提几点建议:

  1. 总是,总是,总是 使用严格;。为了热爱您可能(或可能不)相信的更高权力,use strict;
  2. 每次使用strict;时,你都应该使用warnings;
  3. 另外,认真考虑使用一些缩进。
  4. 另外,考虑为不同的变量使用明显不同的名称。
  5. 最后,你的风格真的很不一致。这是你所有的代码还是你把它们拼凑在一起的?不是想侮辱你或其他任何东西,但我建议不要复制你不理解的代码 - 至少在你复制它之前尝试

现在,您的代码的可读性更高的版本如下所示:

use strict;
use warnings;

print "Input the file name:\n";
my $filename = <STDIN>;
chomp $filename;
open FILE, "<", $filename or die "Can't open file\n\n";
my $newprotein = join "", <FILE>;
close FILE;
$newprotein =~ s/\s//g;
while(1) {
print "enter the motif to be searched:\n";
my $motif = <STDIN>;
last if $motif =~ /^\s*$/;
chomp $motif;
# here I might even use the ternary ?: operator, but whatever
if ($newprotein =~ /$motif/) {
print "found motif\n\n";
}
else {
print "not found\n\n";
}
}

关于perl - 如何在具有不同标题的文件中搜索字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/794501/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com