简体   繁体   English

Perl:解引用数组

[英]Perl: Dereferencing Array

Why does the following code not work in getting into an anonymous array? 为什么以下代码在进入匿名数组时不起作用?

   my @d = [3,5,7];
   print $(@{$d[0]}[0]);  

   # but print $d[0][0] works.

Script 1 (original) 脚本1(原始)

Because it is invalid Perl code? 因为它是无效的Perl代码?

#!/usr/bin/env perl
use strict;
use warnings;

my @d = [3,5,7];
print $(@{$d[0]}[0]); 

When compiled ( perl -c ) with Perl 5.14.1, it yields: 使用Perl 5.14.1编译( perl -c )时,将产生:

Array found where operator expected at xx.pl line 6, at end of line
    (Missing operator before ?)
syntax error at xx.pl line 6, near "])"
xx.pl had compilation errors.

Frankly, I'm not sure why you expected it to work. 坦白说,我不确定您为什么期望它能正常工作。 I can't make head or tail of what you were trying to do. 我不能做你想做的事情的头或尾。

The alternative: 替代方案:

print $d[0][0];

works fine because d is an array containing a single array ref. 因为d是一个包含单个数组引用的数组,所以效果很好。 Thus $d[0] is the array (3, 5, 7) (note parentheses instead of square brackets), so $d[0][0] is the zeroth element of the array, which is the 3. 因此$d[0]是数组(3, 5, 7) (请注意括号而不是方括号),因此$d[0][0]是数组的第零个元素,即3。

Script 2 剧本2

This modification of your code prints 3 and 6: 您对代码的修改显示了3和6:

#!/usr/bin/env perl
use strict;
use warnings;

my @d = ( [3,5,7], [4,6,8] );
print $d[0][0], "\n";
print $d[1][1], "\n";

Question

So the $ in $d[0] indicates that [3,5,7] is dereferenced to the array (3,5,7) , or what does the $ do here? 所以$$d[0]表明[3,5,7]被解除引用数组(3,5,7)或者是什么的$这里做什么? I thought the $ was to indicate that a scalar was getting printed out? 我以为$表示要打印出标量?

Roughly speaking, a reference is a scalar, but a special sort of scalar. 粗略地说,引用是一个标量,但是是一种特殊的标量。

If you do print "$d[0]\\n"; 如果确实print "$d[0]\\n"; you get output something like ARRAY(0x100802eb8) , indicating it is a reference to an array. 您会得到类似ARRAY(0x100802eb8)输出,表明它是对数组的引用。 The second subscript could also be written as $d[0]->[0] to indicate that there's another level of dereferencing going on. 第二个下标也可以写为$d[0]->[0]以指示存在另一级别的取消引用。 You could also write print @{$d[0]}, "\\n"; 您也可以写print @{$d[0]}, "\\n"; to print out all the elements in the array. 打印出数组中的所有元素。

Script 3 脚本3

#!/usr/bin/env perl
use strict;
use warnings;

$, = ", ";

my @d = ( [3,5,7], [4,6,8] );
#print $(@{$d[0]}[0]); 
print @d, "\n";
print $d[0], "\n";
print @{$d[0]}, "\n";
print @{$d[1]}, "\n";
print $d[0][0], "\n";
print $d[1][1], "\n";
print $d[0]->[0], "\n";
print $d[1]->[1], "\n";

Output 输出量

ARRAY(0x100802eb8), ARRAY(0x100826d18), 
ARRAY(0x100802eb8), 
3, 5, 7, 
4, 6, 8, 
3, 
6, 
3, 
6, 

I think you are trying for this: 我认为您正在为此:

${$d[0]}[0]

Though of course there's always the syntactic sugar way, too: 尽管当然也总是有语法糖方式:

$d[0]->[0]

The square bracket constructor creates an anonymous array, but you are storing it in another array. 方括号构造函数创建一个匿名数组,但是您将其存储在另一个数组中。 This means that you are storing the three element array inside the first element of a one element array. 这意味着您将三个元素数组存储在一个元素数组的第一个元素内。 This is why $d[0][0] will return the value 3 . 这就是$d[0][0]返回值3 To do a single level array use the list constructor: 要执行单级数组,请使用列表构造函数:

my @d = (3,5,7);
print $d[0];

If you really mean to create the array inside the outer array then you should dereference the single (scalar) value as 如果您真的要在外部数组中创建数组,则应将单个(标量)值取消引用为

print ${$d[0]}[0].

For more read perldoc perlreftut . 有关更多信息,请阅读perldoc perlreftut

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM