【问题标题】:Perl uninitialized value error after parsing htmlPerl解析html后出现未初始化值错误
【发布时间】:2015-08-03 23:21:51
【问题描述】:

我正在使用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 行。

    在打印中使用未初始化的值 $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 行。

【问题讨论】:

  • 在错误消息中包含更多详细信息可能会有所帮助。
  • 你对 count2-- 所做的事情对我来说没有多大意义。
  • 你不应该压缩你的代码,以至于它不再可读。利用空格。和use warnings。并且不要使用use Switch,该模块已被弃用——请改用 if-else。
  • 此错误消息对应倒数第二个情况:在 /home/adur/workspace/BI7643/GOexamp.pl 第 105 行的字符串 ne 中使用未初始化值 $parsed[101],第 1 行。我之前没有包含错误消息,因为它包含有关我发布的内容之外的工作代码的信息。 Count2-- 将索引向后移动,以便“反馈”不包含在数组切片中。我压缩开关部分的原因是因为我失去了空格的分数。我会用 if-else 替换它。

标签: html perl bioinformatics bioperl


【解决方案1】:

您的意思是不引用第二个和第三个解析调用吗?

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

【讨论】:

  • 这实际上需要下载数据库。我不能使用它,但谢谢。现在主要是解析问题。
猜你喜欢
  • 2014-02-05
  • 2011-03-16
  • 2011-12-13
  • 1970-01-01
  • 1970-01-01
  • 2015-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多