Mintu
Mintu

Reputation: 11

How to assign value to a variable name which is another variable in groovy?

Suppose I have this:

name = "some_name" 
address = "some_value"

and I want to assign value of address variable to value of name variable like below:

some_name = "some_value"

so, that when I do some thing like

this is "${some_name}"

It should print

this is some_value

I am going this as I need to expose key, value pair of maps(which we get as input from user) as variable so that it would be accessible to Jenkins build.

I tried:

"${name}" = "${value}"

But it throws error.

Is there any way in Groovy, I could achieve this?

Upvotes: 1

Views: 3396

Answers (1)

Kaus2b
Kaus2b

Reputation: 845

I have to say this is an odd use case. Maybe theres a better way to solve your problem if elaborate a little more, but in any case this should work for what you are asking.

def name = "some_name"
def address = "some_value"

def myMap = [:]

myMap["${name}"] = address

println "this is ${myMap.some_name}"

Output:

this is some_value

Upvotes: 1

Related Questions