尝试在Perl中循环遍历哈希数组时出错

Ash*_*Ash 1 arrays perl hash flow-control

有几次曾经问过这个问题,但这些答案似乎都不适用于我的情况.

我的代码:

open(FILE, "<", $fileb) or die "File not openable: $!";
while (<FILE>) {
    $filebmeta[$line] = (data => $_, match => -1);
    $line++;
}
close(FILE);
$line = 0;


for my $hashref (@filebmeta) {
     print "$hashref->{data}\n";
}
Run Code Online (Sandbox Code Playgroud)

当我运行这段代码时,它吐出:'不能使用字符串(" - 1")作为HASH引用,而在./partc.pl第152行使用"strict refs".

知道为什么会这样吗?我似乎无法在循环中正确地取消引用散列.

Sod*_*ved 7

数组的元素需要是hashrefs,因此你的assigment语句需要使用大括号:

$filebmeta[$line] = {data => $_, match => -1};
Run Code Online (Sandbox Code Playgroud)

你的代码是为数组分配一个列表,所以它只是获取列表中的最后一个元素-1.因此您的错误消息.