Perl 数组与列表
我在 Perl 中有两个数据结构:
一个数组:
my @array2 = ( "1", "2", "3");
for $elem (@array2) {
print $elem."\n";
}
给出以下输出:
1
2
3
和一个列表:
my @array = [ "1", "2", "3"];
for $elem (@array) {
print $elem."\n";
}
给出以下输出:
ARRAY(0x9c90818)
显然,我想在这两种情况下迭代元素,但为什么第二个解决方案只给我对此数组的引用?
I have two data structures in Perl:
An array:
my @array2 = ( "1", "2", "3");
for $elem (@array2) {
print $elem."\n";
}
Giving me the following output:
1
2
3
And a list:
my @array = [ "1", "2", "3"];
for $elem (@array) {
print $elem."\n";
}
Giving the following output:
ARRAY(0x9c90818)
Obviously, I'd like to iterate over the elements in both cases, but why does the second solution give me only the reference to this array?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在 Perl 中,数组和列表本质上是相同的东西,但您的第二个示例两者都不使用。方括号括住数组引用(标量值),并且使用诸如
my @array = $scalar
之类的内容将标量“分配”给数组相当于我的@array = ($scalar)
。因此,第二个示例中@array
的唯一元素是数组引用。In Perl, arrays and lists are essentially the same thing, but your second example uses neither. Square brackets surround an array reference (a scalar value), and "assigning" a scalar to an array with something like
my @array = $scalar
is equivalent tomy @array = ($scalar)
. Thus, the only element of@array
in your second example is an array reference.Perl 中的列表不是数据结构,它们是源代码中的位置,由它们周围的上下文决定。列表基本上是 Perl 用于移动数据的瞬态结构。您可以使用 Perl 的所有语法与它们交互,但不能将它们作为数据类型使用。最接近列表的数据类型是数组。
当您编写
[1, 2, 3]
时,您所做的是创建一个标量数组引用。该数组引用使用列表1, 2, 3
进行初始化,它与创建命名数组并引用它相同:由于
[...] 构造创建一个标量,您应该将其保存在标量中:
由于您想要操作整个数组,而不仅仅是引用,因此您可以在
$ 前面放置一个
取消引用存储的数组引用。@
arrayLists in Perl are not data structures, they are positions in the source code, determined by the context around them. Lists are basically the transient structures that Perl uses to move data around. You interact with them with all of Perl's syntax, but you can not work with them as a data type. The data type that is closest to a list is an array.
When you write
[1, 2, 3]
what you are doing is creating a scalar array reference. That array reference is initialized with the list1, 2, 3
, and it is the same as creating a named array and taking a reference to it:Since the
[...]
construct creates a scalar, you should hold it in a scalar:Since you want to operate on the whole array, and not just the reference, you place a
@
in front of the$array
which dereferences the stored array reference.方括号用于创建匿名数组。计算时,它返回对此数组的引用,而不是实际的数组值。
括号没有这样的隐藏属性,而是简单地覆盖表达式内部的优先级,就像数学中一样。例如:
实际上是这样计算的:
因为
=
运算符的优先级高于逗号。因此,为了解决这个问题,我们在分配数组时使用括号,如下所示:您的示例:
有点像这样说:
其中
\
用于创建对@tmp数组。
The square brackets are used to create an anonymous array. When evaluated, it returns a reference to this array, not the actual array values.
Parentheses have no such hidden property, but simply override precedence inside expressions, much like in math. For example:
Is actually evaluated like this:
because the
=
operator has higher precedence than the commas. So to get around that, we use parentheses when assigning arrays, like so:Your example:
is somewhat like saying this:
Where the
\
is used to create a reference to the@tmp
array.方括号创建一个匿名数组,用方括号的内容填充该数组,并返回对该数组的引用。换句话说,
与 基本相同
所以代码应该类似于
或
The square brackets create an anonymous array, populate the array with the contents of the brackets, and return a reference to that array. In other words,
is basically the same as
So the code should look like
or
如果您需要更多说明,请参阅相关文档。
为了说明目的,您可以尝试做一些事情:
If you want more clarification, see the relevant documentation.
You could try and do some things for illustration purposes:
@array = ("1","2","3");
这里1,2,3是@array变量的元素。对于前。 $array[0] 为 1 ,$array[1] 为 2 ,$array[2] 为 3。
@array = ["1","2","3"];
Perl 使用 [ ] 来使用匿名数组引用,因此这里基本上只存储一个元素,即数组 ["1","2","3"] 对 @array 变量的引用。例如 $array[0] 是“ARRAY(0x9c90818)”
因此,在打印时它会向您显示参考文献。
@array = ("1","2","3");
Here 1,2,3 is the element of @array variable. For ex. $array[0] is 1 , $array[1] is 2 and $array[2] is 3.
@array = ["1","2","3"];
Perl uses anonymous array references using [ ], so here basically only one element that you are storing, and that is the reference of an array ["1","2","3"] to the @array variable. for ex $array[0] is "ARRAY(0x9c90818)"
Hence while printing it is showing you the references.