Perl语法错误

时间:2014-06-26 16:40:11

标签: perl

我试图找到语法错​​误,但我不能。

错误讯息:

syntax error at /Users/MMM/Desktop/extract2.pl line 52, near "$dbm{"
syntax error at /Users/MMM/Desktop/extract2.pl line 57, near "}"
Execution of /Users/MMM/Desktop/extract2.pl aborted due to compilation errors.

代码:(标记错误行)

#!/usr/bin/Perl
#Extract the accession number and#!/usr /bin/perl
#Extract the accession number and the sequence section from the records in the GenBank file
#Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind

#use warnings;
use BeginPerlBioinfo;
use strict;

# Declare and initialize variables
my $fh;
my $record;
my $dna;
my $annotation;
my $fields;
my $dbm = ' ';
my $answer;
my $offset;
my $LSU = '/Users/MMM/Desktop/FUNGUS/LSU.gb';

# open DBM file, creating if neccessary
unless (dbmopen(my %dbm, 'GB' , 0644)) {
    print "Cannot open DBM file GB with mode 0644\n";
    exit;
}

#Parse GenBank library, saving accession number and sequence in DBM file

$fh = open_file($LSU);

$offset = tell ($fh);

while ( $record = get_next_record($fh))   {
    #Get accession field for this record.

    ($annotation, $dna) = get_annotation_and_dna($record);

    my %fields = parse_annotation( $annotation);

    my $accession =  $fields { 'ACCESSION'};

    # extract just the accession number and sequence from the accession field
    #-- remove any trailing spaces
    $accession =~ s/^ACCESSION\s*//;

    $accession =~ s/\s*$//;

    #store the key value of accession/offset

    my $dbm{$accession} = $offset; # <--- ERROR

    #get offset for the next record

    $offset = tell($fh);
}

2 个答案:

答案 0 :(得分:6)

my $dbm{$accession}语法无效; my只能与变量一起使用,而不能与散列或数组下标一起使用。甚至没有必要使用带有下标的my;只需写下$dbm{$accession} = $offset;

答案 1 :(得分:0)

我认为它与dbm hash的词法范围有关。

你打开它,但它只是在那个范围内宣布。然后你在第52行再次声明它。

初始化变量时

my $dbm = '  ';

应该是

my %dbm;

你使用的所有其他地方%dbm:你不需要将其重新声明为&#39;我的&#39; ,这应该摆脱错误。