# How to add a new element to the array without reassigning?

Hi,

just a simple question, how do I add a new element to the array in Puppet 3.7.2 WITHOUT reassigning the variable?

Suppose I have $array = ['1'] in a small dynamic loop, thus I cannot (or don't want) use new variables to store my new array. I want to add new values to the already existing array$array.

EDIT: Thanks for the reply syphrix. I did try this.

$bla = [1] concat($bla, [2])
notify{"The value is: ${bla}": }  And the result is: Notice: The value is: [1] Notice: /Stage[main]/Scripts/Notify[The value is: [1]]/message: defined 'message' as 'The value is: [1]'  Am I missing something obvious? edit retag close merge delete ## 2 Answers Sort by » oldest newest most voted Hi there, Unfortunately, you cannot reassign variables in Puppet, no matter what. The reason your code is not working is that the concat function returns an rvalue, meaning its return value needs to be assigned to some other variable or it needs to be part of an expression evaluation. So you can do this: $bla = [1]
$blargh = concat($bla, [2])
notify{"The value is: ${blargh}": }  But not this: $bla = [1]
$bla = concat($bla, [2])
notify{"The value is: \${bla}": }

more

Thanks, Greg. I'll mark this answer as it expands more on the subject.

( 2015-09-21 07:23:44 -0500 )edit

You can accomplish this with the concat function from the puppetlabs-stdlib

concat

Appends the contents of multiple arrays onto the first array given. For example:

• concat(['1','2','3'],'4') returns ['1','2','3','4'].
• concat(['1','2','3'],'4',['5','6','7']) returns ['1','2','3','4','5','6','7']. Type: rvalue.
more

Thanks! Quite a shame this restriction exists. Just wanted to be sure if that's the case.

( 2015-09-21 07:21:24 -0500 )edit