0

我无法弄清楚这段代码中的语法错误是什么:

my $questions{$question} = $answer

我正在尝试将每个问题/答案对分配给哈希,但我不知道语法错误是什么。

#!/usr/bin/perl
use warnings;
use CGI;

my $cgi = CGI->new;
my $text = $cgi->param('splash');
my $anslist = "/home/megaoff/www/limages/anslist.txt";
my $unanslist = "/home/megaoff/www/limages/unansquestions.txt";
open(my $ANS, '<', $anslist) or die "Can't open file: $anslist";
open(my $UNANS, '>>', $unanslist) or die "Can't open file: $unanslist";

while(my $line = <$ANS>) {
    my($question, $answer) = split(/\t/, $line);
    my $questions{$question} = $answer;

}

close($ANS) or die "Couldn't close file: $anslist";
close($UNANS) or die "Couldn't close file: $unanslist";
4

1 回答 1

1

首先在while循环上方声明哈希:

my %questions;

然后您可以分配元素,例如(注意缺少my):

$questions{$question} = $answer;
于 2013-05-05T19:02:20.400 回答