Reading dynamic variables

def string varname ="var_" + "${i])"

I can create dynamic variables where varname is a string like var_1 var_2 var_3 etc.
and populate them through an input (name: varname, ....
This works fine

But how could I retrieve the value of a dynamic variable whose name (varname) is in a string into another ordinary variable ? Ideally a setting..

General idea like this but varname is the dynamic name of the variable as a string

MyVar=settings?.varname   or
MyVar = varname

MyVar=var_1      <<< works fine of course

There may be other ways, but settings is a map. I like to use settings["myVarName"]. With a Groovy String, you can use things like "myVar$mum" besides just "myVarName".

1 Like

You want

settings[varName]

You can also do things like

settings["var_$i"]

In fact, your original creation of varname could have simply been

def string varname = "var_${i])"

FYI, you can do this also:

state."var_$k"

2 Likes

Thanks both - works a treat - both 'solutions'

for my use with a dynamic name I used