简体   繁体   English

需要帮助使perl数组脱离标量上下文

[英]Need help getting perl array out of scalar context

I have a perl array I need to store in the following way: 我有一个perl数组,需要通过以下方式存储:

 $self->{spec}->{allImages} = @allImages;

Then I need to retrieve the contents later: 然后我需要稍后检索内容:

 print Dumper($self->{spec}->{allImages});

This yields: 这样产生:

 $VAR1 = 10;

(the number of items in the array). (数组中的项目数)。

How can I break out of scalar context and get $self->{spec}->{allImages} back as a list? 如何突破标量上下文并获得$ self-> {spec}-> {allImages}作为列表?

Each hash value can only be a scalar. 每个哈希值只能是一个标量。

You must store a reference to the array: 您必须存储对数组的引用:

$self->{spec}->{allImages} = \@allImages;

http://perldoc.perl.org/perlreftut.html will give you more tutorial. http://perldoc.perl.org/perlreftut.html将为您提供更多教程。

You need to change the assignment: 您需要更改分配:

$self->{spec}->{allImages} = \@allImages;

This creates an array-ref that you can use. 这将创建一个可以使用的array-ref。

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

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