简体   繁体   English

Perl-遍历数组值的哈希

[英]Perl - Iterate through hash of array values

I have a data structure which I want to iterate through, then push to another array for temporary storage. 我有一个要遍历的数据结构,然后推送到另一个数组进行临时存储。

Each view in the hash has an array of fieldsets. 哈希中的每个视图都有一组字段集。

1 = {
   fieldset => ('package', 'payment'),
},

2 => {
   fieldset => ('address, 'review'),
}
3 => {
   fieldset => ('confirm'),
}

etc. 等等

I want to grab all these values, and comma separate them into an another array, so I can see which steps the customer has left. 我想获取所有这些值,然后用逗号将它们分成另一个数组,这样我就可以看到客户离开了哪些步骤。

if I try 如果我尝试

@array = $value->{fieldsets} 

it only grabs the first item. 它只抓取第一项。 How do I grab all of them? 我该如何抓住所有人?

Let me know if I haven't explained it in enough depth. 让我知道我是否没有足够深入的解释。

Hash values contain scalars. 哈希值包含标量。

1 = {
   fieldset => ('package', 'payment'),
},

flattens to: 展平为:

1 = {
   fieldset => 'package',
   payment => undef,
},

You want: 你要:

1 = {
   fieldset => ['package', 'payment'],
},

To store an arrayref scalar, and access by dereferencing the contents of the fieldsets key: 要存储arrayref标量,并通过取消引用字段集键的内容进行访问:

@array = @{$value->{fieldset}}

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

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