我正在使用 Bioperl
查找基因的 GOterms
。
我检索一个 html
文件,将其转换为文本,删除所有多余的空格和换行符,然后尝试遍历生成的数组。
但是,我在访问数组中未初始化的值时不断收到错误。我进行了多次检查以确保数组不为空并且不会超出范围。我怎样才能摆脱这个错误?
我以更易读的格式重新发布了代码。感谢您的帮助。
它似乎成功地从 html 中解析出了正确的数据,所以我不知道出了什么问题。
#!/usr/bin/perl -w
use strict;
use LWP::Simple;
use HTML::TreeBuilder;
use HTML::FormatText;
my $URL = get("http://amigo.geneontology.org/amigo/term/GO:0000001");
my $Format = HTML::FormatText->new;
my $TreeBuilder = HTML::TreeBuilder->new;
$TreeBuilder->parse($URL);
my $Parsed = $Format->format($TreeBuilder);
print "$Parsed";
my @parsed = split( /[ ]{2,}|(\n+)|(\r+)/, $Parsed );
if ( @parsed == 1 ) { return; }
my %termhash;
my $count = 0;
while ( $count < @parsed ) {
if ( defined $parsed[$count] && $parsed[$count] eq 'Name' ) {
my $count2 = $count;
while ( ( $parsed[$count2] ne 'Feedback' ) && ( $count2 < @parsed ) ) {
$count2++;
}
$count2--;
@parsed = @parsed[ $count .. $count2 ]; # Gets the slice of the array needed
last;
}
$count++;
}
if ( @parsed <= 1 ) { return; }
print "\n";
print @parsed;
$count = 0;
while ( $count < @parsed ) {
if ( $parsed[$count] eq 'Name' ) {
while ( $parsed[$count] ne 'Ontology' && ( $count < @parsed )) {
$termhash{'Name'} .= $parsed[$count];
$count++;
}
}
if ( $parsed[$count] eq 'Ontology' ) {
while ( $parsed[$count] ne 'Synonyms' && ( $count < @parsed )) {
$termhash{'Category'} .= $parsed[$count];
$count++;
}
}
if ( $parsed[$count] eq 'Synonyms' ) {
while ( $parsed[$count] ne 'Definition' && ( $count < @parsed )) {
$termhash{'Aliases'} .= $parsed[$count];
$count++;
}
}
if ( $parsed[$count] eq 'Definition' ) {
while ( $parsed[$count] ne 'Comment' && ( $count < @parsed )) {
$termhash{'Definition'} .= $parsed[$count];
$count++;
}
}
if ( $parsed[$count] eq 'Comment' ) {
while ( $parsed[$count] ne 'History' && ( $count < @parsed )) {
$termhash{'Comment'} .= $parsed[$count];
$count++;
}
}
if ( $parsed[$count] eq 'History' ) {
while ( $parsed[$count] ne 'Subset' && ( $count < @parsed )) {
$termhash{'Version'} .= $parsed[$count];
$count++;
}
}
if ( $parsed[$count] eq 'Subset' ) {
while ( ( $parsed[$count] ne 'Community' ) && ( $count < @parsed ) ) {
$count++;
}
}
if ( $parsed[$count] eq 'Community' ) {
while ( ( $parsed[$count] ne 'Related' ) && ( $count < @parsed ) ) {
$count++;
}
}
if ( $parsed[$count] eq 'Related' ) {
for ( $count < @parsed ) {
$termhash{'Definition references'} .= $parsed[$count];
$count++;
}
}
}
if ( $termhash{'Definition'} =~ m/OBSOLETE/ ) { $termhash{'Is obsolete'} = 1 }
else { $termhash{'Is obsolete'} = 0 }
#print %termhash;
主要错误消息是:
在字符串 ne 中使用未初始化的值 $parsed[127] /home/adur/workspace/BI7643/ParseGOhtml.pl 第 23 行。
在 print 中使用未初始化的值 $parsed[1] /home/adur/workspace/BI7643/ParseGOhtml.pl 第 35 行。
在字符串 ne 中使用未初始化的值 $parsed[1] /home/adur/workspace/BI7643/ParseGOhtml.pl 第 42 行。
在连接 (.) 或字符串中使用未初始化的值 $parsed[1] 位于/home/adur/workspace/BI7643/ParseGOhtml.pl 第 41 行。
在字符串 ne 中使用未初始化的值 $parsed[17] /home/adur/workspace/BI7643/ParseGOhtml.pl 第 48 行。
在连接 (.) 或字符串中使用未初始化的值 $parsed[17] 位于/home/adur/workspace/BI7643/ParseGOhtml.pl 第 47 行。
在字符串 ne 中使用未初始化值 $parsed[29] /home/adur/workspace/BI7643/ParseGOhtml.pl 第 54 行。
在连接 (.) 或字符串中使用未初始化的值 $parsed[29] 位于/home/adur/workspace/BI7643/ParseGOhtml.pl 第 53 行。
在字符串 ne 中使用未初始化的值 $parsed[41] /home/adur/workspace/BI7643/ParseGOhtml.pl 第 60 行。
在连接 (.) 或字符串中使用未初始化的值 $parsed[41] 位于/home/adur/workspace/BI7643/ParseGOhtml.pl 第 59 行。
在字符串 ne 中使用未初始化的值 $parsed[79] /home/adur/workspace/BI7643/ParseGOhtml.pl 第 66 行。
在连接 (.) 或字符串中使用未初始化的值 $parsed[79] 位于/home/adur/workspace/BI7643/ParseGOhtml.pl 第 65 行。
在字符串 ne 中使用未初始化的值 $parsed[83] /home/adur/workspace/BI7643/ParseGOhtml.pl 第 72 行。
在连接 (.) 或字符串中使用未初始化的值 $parsed[83] 位于/home/adur/workspace/BI7643/ParseGOhtml.pl 第 71 行。
在字符串 ne 中使用未初始化的值 $parsed[95] /home/adur/workspace/BI7643/ParseGOhtml.pl 第 77 行。
在字符串 ne 中使用未初始化的值 $parsed[107] /home/adur/workspace/BI7643/ParseGOhtml.pl 第 82 行。
最佳答案
您的意思是不引用第二个和第三个解析调用吗?
htmlparse('GO:0000001');
htmlparse(GO:0000002);
htmlparse(GO:0000003);
应该是;
htmlparse('GO:0000001');
htmlparse('GO:0000002');
htmlparse('GO:0000003');
另外,请确保将以下内容添加到文件顶部
use warnings;
use diagnostics;
如果问题仍然存在,请发回生成的错误消息。
此外,根据 http://geneontology.org/page/lead-database-guide您可以直接查询在线数据库,而无需解析 html,他们使用他们的工具提供了以下示例;
GOshell.pl -d go_latest -dbuser go_select -dbauth amigo -port 4085 -h mysql.ebi.ac.uk
相关cpan信息; http://cpansearch.perl.org/src/SJCARBON/go-db-perl-0.04/doc/go-db-perl-doc.html
关于html - Perl解析html后出现未初始化值错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30388177/