AnsweredAssumed Answered

Value of dynamic generated variable in GEL

Question asked by Fabio Cresta Employee on Apr 22, 2014
Latest reply on Apr 22, 2014 by gcubed

Hi All.

I want to share something that I just discovered and can be of help fo others.

I had a number of variables with a name syntax like 'var1', 'var2', ... 'varn'. Each of them containing a value.

I neede to create a loop to extract the value of each variable. So I wanted to create a variable to store the name of the variable linked to the current loop index value, and then extract the value.

Something like

for each i=1 to 5

print value of var[i]

I could have done it with an array but, for my purpose, that was not suitable.

So I discovered the statement context.getVariable(var) that extraxct the value of the variable 'var'.

Here is an example:


<!-- Sample to extract a value of a variable whose name is dynamically generated
   Fabio Cresta - crefa01 - May 2014
<gel:script xmlns:SOAP-ENV=""

     <!-- set values to variables -->
     <core:set var="var1" value="value1"/>
     <core:set var="var2" value="value2"/>
     <core:set var="var3" value="value3"/>
     <core:set var="var4" value="value4"/>
     <core:set var="var5" value="value5"/>

     <!-- execute a for each loop that:
         1. creates a variable name based on the current index value
         2. prints the value of the variable generated -->

     <core:forEach indexVar="i" begin="1" end="5">
         <core:set var="dynVar"     value="var${i}"/>
         <gel:out>Index: ${i} - variable = ${dynVar} -  variable value = ${context.getVariable(dynVar)}</gel:out>