Perl for循环错误

1 arrays perl perl-data-structures

我是Perl的新手,我试图使用Strawberry perl 5,版本16执行下面编码的简单程序:

#!usr/bin/perl

use warnings;
use strict;

my @array= {1,2,3,5,7,9};
my $i;

foreach $i (@array)
{
print qq(Element is $i\n);
}
Run Code Online (Sandbox Code Playgroud)

我得到以下输出:

Element is HASH(0x3f8b4c)
Run Code Online (Sandbox Code Playgroud)

但是我应该收到的输出是:

Element is 1
Element is 2
Element is 3
Element is 5
Element is 7
Element is 9.
Run Code Online (Sandbox Code Playgroud)

感谢你的帮助.

amo*_*mon 10

要初始化数组,请使用类似的列表

my @array = (1, 2, 3, 5, 7, 9);
Run Code Online (Sandbox Code Playgroud)

注意:parens只是排除优先级,它们不是特殊的数组语法.

Curlies分隔一个匿名的hashref,就像

my $foobar = {
  foo => "bar",
  baz => "qux",
};
Run Code Online (Sandbox Code Playgroud)

所以发生的事情就是你为你的数组分配了一个匿名hashref的列表,就像

my @array = ({
  1 => 2,
  3 => 5,
  7 => 9,
})
Run Code Online (Sandbox Code Playgroud)

本来会有用的.