@H_502_4@
我有以下代码:
@a = ((1,2,3),("test","hello")); print @a[1]
我期待它打印
testhello
但它给了我2.
抱歉新手问题(Perl对我来说有点不自然),但为什么会发生这种情况,我怎样才能得到我想要的结果呢?
@H_502_4@解决方法
Perl构造@a的方式是等同于你的写作,
@a = (1,3,"test","hello");
这就是为什么当你通过编写@a [1](真的应该是$a [1])来索引索引1的值时,你会得到2.为了证明这一点,如果你要做以下事情,
use strict; use warnings; my @a = ((1,"hello")); my @b = (1,"hello"); print "@a\n"; print "@b\n";
两者都打印相同的行,
1 2 3 test hello 1 2 3 test hello
你想要的是在你的数组中创建匿名数组 – 像这样,
my @c = ([1,3],["test","hello"]);
如果你写下面的,
use Data::Dumper; print Dumper $c[1];
你会看到这个打印,
$VAR1 = [ 'test','hello' ];@H_502_4@ @H_502_4@