Hello,
I have a question regarding GE's MEL language.
Is it possible to have use dynamically determined variable names?
Example:
I have a document variable named "item_1" for a drop down list, and I want to target this item to populate its contents dynamically. I have several items named "Item_" which are all related (attributes pertaining to one item).
Execute: getValuesForItem (item, 1)
Function:
fn getValuesForItem (item, num) {
item_ + num = "Value 1, Value 2, Value 3"
}
Any time I try to use item + num it comes out as "" in the MEL trace.
If I try str(item_, num) the MEL trace shows "item_1", but it looks like its just treating it as a string instead of a document variable.
I.E. When it gets to the assignment section, in the MEL trace it looks like:
"item_ 1" == "Value 1, Value 2, Value 3" instead of item_ 1 = "Value 1, Value 2, Value 3"
I think you want to use eval(), it should be in the help files, but here is how to use it.
eval("item_" + num + " = \"value1, value2, value3\"")
Thanks, this was helpful.
I was able to address local and global variables with the same postfix with this:
{fn updateElement(name, num, value){
eval(name + num + " = " + "'" + value + "'")
}}
It doesn't appear to work on DOCUMENT variables though - it seems those operate a little differently.
It should, you will have to get everything inside a set of quotes though, which can be tricky. You want it to evaluate this -
{eval("document.variable_name = "Value"")}
So setup might be closer to this -
{eval("document." + name + num + " = \"" + value + "\"")}
If your values could contain backslashes or quotes, you may need to escape those. Test to see if they work... I think I've had to do something like this before:
eval("DOCUMENT.E" + e + "='" + replacestr(replacestr(exercises[e],"\\","\\\\"),"'","\\'") + "'")
Thanks for the information about escape characters Kevin!
I figured there were some secret characters to making this possible.
Hmm I've been trying this and I thought the escape characters would work, but I'm still stuck.
Here some example code I would expect to work:
DOCUMENT.GROUP_1 = "Hello"
groupValue = eval("document." + "group_" + 1)
Here's the MEL trace:
02/17/2016 09:44:16.922-execute> Document.GROUP_1_4480_1455298475_69 = "Hello"
02/17/2016 09:44:16.922-results> "Hello"
02/17/2016 09:44:16.922-execute> "document." + "group_"
02/17/2016 09:44:16.922-results> "document.group_"
02/17/2016 09:44:16.922-execute> "document.group_" + 1
02/17/2016 09:44:16.922-results> "document.group_1"
02/17/2016 09:44:16.922-execute> call EVAL("document.group_1")
02/17/2016 09:44:16.922->>
Fire trigger: document.group_1
02/17/2016 09:44:16.922-execute>> Document.group_1
02/17/2016 09:44:16.922-results>> NULL
02/17/2016 09:44:16.922-execute>> end
02/17/2016 09:44:16.922-results>> NULL
So it looks like it performed eval("document.group_1"), but it didn't yield my example value of "Hello" that I stored in Document.Group_1.
That is another trap of the eval() function. In VFE when you create a field by default it is NOT available to other forms. To accomplish this they add a bunch of seemingly random numbers at the end of the variable name (above it is the part: 4480_1455298475_69) when you save the form and it creates a clinical kit. For eval to work with these fields you have to check the box in VFE that says "Make available to other forms," then VFE will not add the numbers and document.group_1 will stay document.group_1 and not turn into document.group_1_4480_1455298475_69.
Thank You Thank You Thank You!!!!
This was my puzzle piece missing for being able to dynamically address DOCUMENT variables.
Now I can use a similar function to the one I previously posted for DOCUMENT variables.
I've marked my target variables as "make available on other forms" and now everything works as you and Kevin were indicating.
again, Thank You Thank You Thank You!!