"Test", "Param1" => 1, "Data1"-6ren">
gpt4 book ai didi

arrays - 数组引用数组的哈希数组

转载 作者:行者123 更新时间:2023-12-02 00:58:26 24 4
gpt4 key购买 nike

我在访问数据时遇到问题,如问题标题所述。
数据结构如下:

my @structure = (
{
"Name" => "Test",
"Param1" => 1,
"Data1" => \@test1,
"Data2" => [\@test3],
"Data3" => [\@test1, \@test2],
},
...
);

我需要访问数组的第一个(#0)元素@test3
我尝试过这样的事情:

my @array   = @{$_->{'Data2'}}[0];
print $array[0];

但是随后我得到了对数组 (ARRAY(0x239c3c)) 的引用,而不是所需的含义。我觉得这里有些全局性的东西我不明白。
您不能解释一下我应该如何以及为什么要解决所需的含义吗?
非常感谢。

最佳答案

use strict;
use warnings;

my @test1 = (1,2,3);
my @test2 = (4,5,6);
my @test3 = (7,8,9);

my @structure = (
{
"Name" => "Test",
"Param1" => 1,
"Data1" => \@test1,
"Data2" => [\@test3],
"Data3" => [\@test1, \@test2],
}
);

print $structure[0] # $structure is an array, you want first element...
->{'Data2'} # ...which is a hash reference, you want value for 'Data2'...
->[0] # ...which is holding an array reference to (one element) array @test3...
->[0]; # ...which is an array reference, and you want first element

打印7

关于arrays - 数组引用数组的哈希数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8647522/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com