【发布时间】:2017-10-28 01:14:38
【问题描述】:
这是工作的一部分。在这一部分中,我正在尝试编写一个程序来创建哈希。键是文件中的登录号,值是整行。但是,该程序给了我一个警告。代码是:
#!/usr/bin/perl
#psuedocode:
#open file1, store uniport accesion as key and the line as value
#open file2, store uniport accesion as key and the line as value which lines contain "IDA"
#compare keys in two hashes, find out matched keys
#print out lines from file2 that match
use strict;
use warnings;
use feature qw(say);
my $infile1 = "geneIDs3_MouseToUniProtAccessions.txt";
my $inFH1;
open ($inFH1, "<", $infile1) or die join (" ", "Can't open", $infile1, "for reading:", $!);
my @array1 = <$inFH1>;
close $inFH1;
shift @array1;
my %geneID1;
for ($a = 0; $a < scalar @array1; $a++){
chomp $array1[$a];
$array1[$a] =~ /.*?\t(.*?)\t.*/;
$geneID1{$1} = $array1[$a];
#say ("$1", '->', "$geneID1{$array1[$a]}"); #test if the hash has been successfully created, however it doesn't
#say $array1[$a]; #test if the program can recognize the elements, it does
}
文件geneIDs3_MouseToUniProtAccessions.txt 包含1,000 行,因此警告很多。前两行是:
From To Species Gene Name
PNMA3 Q9H0A4 Homo sapiens paraneoplastic antigen MA3
警告如下:
Use of uninitialized value within %geneID1 in string at match_for_part_III_10.pl line 24.
Q9H0A4->
我找到了解决方案:改用 while 循环。它不仅有效,而且更优雅。新代码是:
#!/usr/bin/perl
#psuedocode:
#open file1, store uniport accesion as key and the line as value
#open file2, store uniport accesion as key and the line as value which lines contain "IDA"
#compare keys in two hashes, find out matched keys
#print out lines from file2 that match
use strict;
use warnings;
use feature qw(say);
my $infile1 = "geneIDs3_MouseToUniProtAccessions.txt";
my $inFH1;
open ($inFH1, "<", $infile1) or die join (" ", "Can't open", $infile1, "for reading:", $!);
my %geneID1;
while (<$inFH1>){
$_ =~ /.*?\t(.*?)\t.*/;
$geneID1{$1} = $_;
say ("$1", '->', "$geneID1{$1}");
}
close $inFH1;
感谢大家的大力帮助!
【问题讨论】:
-
警告=您没有处理某些情况,可能缺少数据。你为什么不把它们打印出来看看。
-
@zdim 其他部分工作得很好。我不需要声明
$a,请阅读我上一个问题stackoverflow.com/questions/46739301/…的评论 -
@zdim,
$a总是被声明。 -
@Wenjia Zhai,你知道
$a很特别,你还用吗?坏你! -
Re "我找到了解决方案:改用 while 循环",不,这并没有解决问题,而 你 没有“找到”它的那个。如上所述,问题在于您使用了错误的密钥。 (你分配给
$geneID1{$1},但是你查看$geneID1{$array1[$a]}的内容)
标签: perl