Arrays 使用perl将标识符列表与另一个列表匹配
我被困在如何将一个列表循环到另一个列表上。也许我不是在寻找合适的词,但我被卡住了,如果能在我的代码上得到任何帮助,我将不胜感激 我检查了这个线程,但是在运行脚本时仍然出现错误 数据库文件Arrays 使用perl将标识符列表与另一个列表匹配,arrays,perl,foreach,Arrays,Perl,Foreach,我被困在如何将一个列表循环到另一个列表上。也许我不是在寻找合适的词,但我被卡住了,如果能在我的代码上得到任何帮助,我将不胜感激 我检查了这个线程,但是在运行脚本时仍然出现错误 数据库文件 chr1 1692239 1692249 AH_GARP2_comp198_c0_seq1 chr1 2233934 2233944 CS_GARP2_comp323_c0_seq1 chr1 5993434 5993444 CS_GARP2_comp635_c0_seq1 chr1 6
chr1 1692239 1692249 AH_GARP2_comp198_c0_seq1
chr1 2233934 2233944 CS_GARP2_comp323_c0_seq1
chr1 5993434 5993444 CS_GARP2_comp635_c0_seq1
chr1 6198157 6198167 CS_GARP2_comp115_c0_seq1
chr1 6465781 6465791 JB_GARP2_comp560_c0_seq1
chr1 7827923 7827933 JB_GARP2_comp855_c0_seq1
chr1 7920939 7920949 AA_GARP2_comp614_c0_seq1
chr1 7964000 7964010 CS_GARP2_comp717_c0_seq1
chr1 9314857 9314867 AH_GARP2_comp237_c0_seq1
chr1 9654532 9654542 AH_GARP2_comp632_c0_seq1
查询文件
name1 CS_GARP2_comp635_c0_seq1
name2 JB_GARP2_comp855_c0_seq1
name3 AH_GARP2_comp198_c0_seq1
name4 AH_GARP2_comp237_c0_seq1
我的代码
#!/usr/bin/perl5.16.2
use 5.16.2;
use lib '/users/ec1/perl5/lib/perl5/';
use warnings;
use strict;
my $filename = shift; ## database
my $filename2= shift; ## list of ids
open (DB, '<', $filename ) || die "Unable to open: $!";
open (I , '<', $filename2) || die "Unable to open: $!";
my @DB;
while (<DB>) {
chomp;
my @DB = split /\t/, $_; ## define as list
#print "@DB[0,1,2,3]\n";
}
while (my $line = <I>) {
chomp $line;
my ($name, $id) = split /\t/, $line;
if ($DB[3] =~ /$id/) {print "$DB[0]\t$DB[1]\t$DB[2]\t$DB[3]\n";
} else {print "na\n"}
}
#/usr/bin/perl5.16.2
使用5.16.2;
使用lib'/users/ec1/perl5/lib/perl5/';
使用警告;
严格使用;
我的$filename=shift;##数据库
我的$filename2=shift;##ID列表
打开(DB,将DB放入哈希(%DB
)以简化搜索
use strict; use warnings;
my $filename = shift; ## database
my $filename2= shift; ## list of ids
my %DB;
open (DB, '<', $filename ) || die "Unable to open: $!";
while (<DB>) {
chomp;
my @row = split( /[ \t]+/, $_); ## define as list
die "expected four items in db file - line $.\n" unless @row == 4; # expect four elements in a row
die "duplicate id in db file - line $." if exists $DB{$row[3]};
$DB{$row[3]} = \@row;
}
close DB;
open (I , '<', $filename2) || die "Unable to open: $!";
while (<I>) {
chomp;
my ($name, $id) = split /[ \t]+/, $_;
if(exists ($DB{$id})) {
my @row = @{$DB{$id}};
print join("\t", @row), "\n";
} else {
print "na\n"
}
}
close(I);
使用严格;使用警告;
我的$filename=shift;##数据库
我的$filename2=shift;##ID列表
我的%DB;
打开(DB),您的方法的问题是my@DB
内部while
循环创建了一个词法范围,因此它将始终包含最后一行的内容,而该内容在循环外部将不可用
您应该将id文件读入到哈希映射中,并检查db文件的行是否作为哈希中的键存在
!/usr/bin/perl
使用警告;
严格使用;
使用自动模具;
my($dbfile,$idfile)=@ARGV;
打开我的$id\u fh,'哇!谢谢!在$DB{$row[3]}=\@row;
之前,我还没有学会如何传递对数组的引用;
我将阅读更多关于它的内容,更复杂的版本甚至可能处理多行共享同一id的数据文件[行数组散列,而不是行散列]谢谢!所以创建散列不一定需要{key,value}配对?@oolongoni不,+$ids{$}
正在为每个键分配count作为值。如果我想搜索数据库文件中的整行,该怎么办?这很有效,但我得到了一个警告“多维语法$fld[0,3]在hashlookup.pl第20行不受支持。”while(){my@fld=(split/\s+/);打印“na\n”和next,除非$ids{$fld[0,3]};打印“$”;}
@oolongoni调用列表时,只需使用@fld[0,3]
告诉perl即可。$
始终是标量。