snakespan
snakespan

Reputation: 1211

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.

Upvotes: 0

Views: 90

Answers (1)

Oesor
Oesor

Reputation: 6642

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:

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

Upvotes: 1

Related Questions