如何在 perl 中创建多维数组? [英] How can I create a multi-dimensional array in perl?
问题描述
我以这种方式创建了一个多维数组:
I was creating a multi-dimensional array this way:
#!/usr/bin/perl
use warnings;
use strict;
my @a1 = (1, 2);
my @a2 = (@a1, 3);
但结果我还是得到了一个一维数组...
But it turns out that I still got a one-dimensional array...
Perl 的正确方法是什么?
What's the right way in Perl?
推荐答案
你得到一个一维数组,因为数组 @a1
在括号内展开.所以,假设:
You get a one-dimensional array because the array @a1
is expanded inside the parentheses. So, assuming:
my @a1 = (1, 2);
my @a2 = (@a1, 3);
那么你的第二个语句等价于my @a2 = (1,2,3);
.
Then your second statement is equivalent to my @a2 = (1,2,3);
.
创建多维数组时,您有几个选择:
When creating a multi-dimensional array, you have a few choices:
- 直接分配每个值
- 取消引用现有数组
- 插入引用
第一个选项基本上是$array[0][0] = 1;
,不是很精彩.
The first option is basically $array[0][0] = 1;
and is not very exciting.
第二个是这样做的:my @a2 = (\@a1, 3);
.请注意,这会引用数组 @a1
的命名空间,因此如果您稍后更改 @a1
,@a2
中的值将也改变.它并不总是推荐的选项.
The second is doing this: my @a2 = (\@a1, 3);
. Note that this makes a reference to the namespace for the array @a1
, so if you later change @a1
, the values inside @a2
will also change. It is not always a recommended option.
第二个选项的变体是这样做:my @a2 = ([1,2], 3);
.括号将创建一个匿名数组,该数组没有命名空间,只有内存地址,并且只存在于@a2
中.
A variation of the second option is doing this: my @a2 = ([1,2], 3);
. The brackets will create an anonymous array, which has no namespace, only a memory address, and will only exist inside @a2
.
第三个选项,有点模糊,是这样做的: my $a1 = [1,2];我的@a2 = ($a1, 3);
.它将做与 2 完全相同的事情,只是数组引用已经在一个名为 $a1
的标量变量中.
The third option, a bit more obscure, is doing this: my $a1 = [1,2]; my @a2 = ($a1, 3);
. It will do exactly the same thing as 2, only the array reference is already in a scalar variable, called $a1
.
注意()
和[]
在赋值给数组时的区别.括号 []
创建一个匿名数组,它返回一个数组引用作为标量值(例如,可以由 $a1
或 $a2[0]
).
Note the difference between ()
and []
when assigning to arrays. Brackets []
create an anonymous array, which returns an array reference as a scalar value (for example, that can be held by $a1
, or $a2[0]
).
另一方面,除了改变运算符的优先级之外,括号实际上什么都不做.
Parentheses, on the other hand, do nothing at all really, except change the precedence of operators.
考虑这段代码:
my @a2 = 1, 2, 3;
print "@a2";
这将打印 1
.如果你use warnings
,你也会得到一个警告,比如:Useless use of a constant in void context
.基本上,会发生这种情况:
This will print 1
. If you use warnings
, you will also get a warning such as: Useless use of a constant in void context
. Basically, this happens:
my @a2 = 1;
2, 3;
因为逗号 (,
) 的优先级低于等号 =
.(请参阅 perldoc perlop 中的运算符优先级和关联性".)
Because commas (,
) have a lower precedence than equal sign =
. (See "Operator Precedence and Associativity" in perldoc perlop.)
括号简单地否定了 =
和 ,
的默认优先级,并将 1,2,3
组合在一个列表中,然后传递给 @a2
.
Parentheses simply negate the default precedence of =
and ,
, and group 1,2,3
together in a list, which is then passed to @a2
.
所以,简而言之,方括号 []
有一些魔力:它们创建匿名数组.括号,()
,只是改变优先级,就像在数学中一样.
So, in short, brackets, []
, have some magic in them: They create anonymous arrays. Parentheses, ()
, just change precedence, much like in math.
文档中有很多内容需要阅读.这里有人曾经向我展示了一个非常好的取消引用链接,但我不记得它是什么.在 perldoc perlreftut 中,您将找到有关参考的基本教程.在 perldoc perldsc 中,您会找到有关数据结构的文档(感谢 Oesor 提醒我).
There is much to read in the documentation. Someone here once showed me a very good link for dereferencing, but I don't recall what it was. In perldoc perlreftut you will find a basic tutorial on references. And in perldoc perldsc you will find documentation on data structures (thanks Oesor for reminding me).
这篇关于如何在 perl 中创建多维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!