I am using Puppet 2.7 and I need to convert an array to comma separated list.
$hosts_fqdn= ['host1','host2','host3']
And I need to convert it to desired result: 'host1,host2,host3'
I guess that Puppet 3.2 offers lambda expression - reduce
. But unfortunately that is not possible with 2.7.
To convert an array to a comma-separated string, call the join() method on the array, passing it a string containing a comma as a parameter. The join method returns a string containing all array elements joined by the provided separator.
The simplest way to convert an array to comma separated String is to create a StringBuilder, iterate through the array, and add each element of the array into StringBuilder after appending the comma.
List<String> items = Arrays. asList(commaSeparated. split(",")); That should work for you.
Arrays have their own implementation of toString method that returns a comma-separated list of elements.
Function join
from puppetlabs/stdlib:
join($hosts_fqdn,',')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With