Is it possible to create an array of hashes

719 views
Skip to first unread message

Raghu Ram Baisani

unread,
Aug 26, 2020, 1:45:43 PM8/26/20
to Puppet Users
Hi Guys

Some one help me solving this issue.
Example:
[
    {
        name => raghuram,
        age  => 22
    },
    {
        name => vinay
        age  => 25
    }
]
the idea is to iterate through an existing hash and create new hash with some modifications and add it to array.
Thanks in advance

Benjamin Ridley

unread,
Aug 26, 2020, 5:42:11 PM8/26/20
to puppet...@googlegroups.com
Hi Raghu,

Yes this is possible. Your example looks workable, but don't forget to add commas after the values of the hash. 

Check out Puppet's 'reduce' function, it iterates over data and allows you to return a new structure after all the iteration which could be your new hash. Alternatively it might be worth writing something in Ruby if Puppet's immutable variables get in the way which may happen if you're heavily modifying your hash. 

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/32ee1353-0cd3-47fa-af66-c3ef976dcdean%40googlegroups.com.

Raghu Ram Baisani

unread,
Aug 27, 2020, 12:01:03 AM8/27/20
to Puppet Users

Thanks for the reply.
I didn't understand what you are trying to say.
The code is something like this:
$array_inputs = []
$vm_data.each |$region| {
$hash_region = {
 location => $region['location'],
 vm_list  => $region['vmlist'],
 customer_id => "customer-${$region['location']}",
api_key => "api-${$region['location']}",
secret_key => "secret-${$region['location']}",
}
 $array_inputs <<  $hash_region

}
return $array_inputs
}
I'm not able to add a new hash into the array after every iteration.
Can you please provide some resolution for this.
Thanks in advance
Raghuram Baisani

Jesse Reynolds

unread,
Aug 27, 2020, 3:17:38 AM8/27/20
to puppet...@googlegroups.com
Hi Raghu, 

It sounds like you want to use the map function: https://puppet.com/docs/puppet/6.17/function.html#map 

Here's an example of how to use it with your data structure: 

$array_inputs = $vm_data.map |$region| {

  $hash_region = {
    location    => $region['location'],
    vm_list     => $region['vmlist'],
  }
}

I have pulled out the following lines though, as the variable name interpolation within a variable name is probably not going to work how you're expecting it to:

    customer_id => "customer-${$region['location']}",
    api_key     => "api-${$region['location']}",
    secret_key  => "secret-${$region['location']}",

For those lookups you are probably best off creating a separate lookup hash perhaps like this:

$region_data = {
  'apac' => {
    'customer_id' => 'xxx',
    'api_key'     => 'yyy',
    'secret_key'  => 'zzz',
  }
}

customer_id => "customer-${region_data['apac']['customer_id']}"

Cheers
Jesse






--
Jesse Reynolds
Principal Professional Services Engineer
Melbourne, Australia
Reply all
Reply to author
Forward
0 new messages