Page 1 of 1

Can we use dynamic variable names?

Posted: 02 Sep 2015 02:44
by natong

Code: Select all

cid = 123456;
bssid = "12:34:56:78:9A:BC";
global_wifi_bssid = bssid;    // 12:34:56:78:9A:BC
global_wifi_bssid = replace(global_wifi_bssid, ":", "_");   // 12_34_56_78_9A_BC
How to use dynamic variable names as global_12_34_56_78_9A_BC ?
Is these work?

Code: Select all

addElement(global_{global_wifi_bssid}, cid);

Re: Can we use dynamic variable names?

Posted: 02 Sep 2015 05:11
by kintrupf
I htink what you're looking for is a map, a list with name/value pairs. There are a couple of functions for this:

Map newMap()
Returns a new empty map
Map newMapFromValues(String key1, Object value1, ...)
Returns a new map initialized to contain the specified keys and values.
Boolean isEmpty(Map map)
Returns whether the map contains any elements or not.
Number length(Map map)
Returns the number of elements in map.
Map copyMap(Map map)
Returns a copy of specified map (flat copy).
Map addMapEntry(Map map, String key, Object value)
Adds a new entry key->value to the map and returns the map. Returns a new map when the map should not exist yet.",
Object getMapValue(Map map, String key)
Returns the object for key or null when no mapping for key exists.
Object getMapValue(Map map, String key, Object default)
Returns the object for key or default when no mapping for key exists or map is null.
Object removeMapEntry(Map map, String key)
Removes the entry for the given key and returns the value.
List getMapKeys(Map map)
Returns a list of the keys of map.
List getMapValues(Map map)
Returns a list of the values of map.

Your code might look like this:

Code: Select all

cid = 123456;
bssid = "12:34:56:78:9A:BC";
global_wifi_list = newMap();
addMapEntry(global_wifi_list, bssid, cid);
...

Re: Can we use dynamic variable names?

Posted: 06 Oct 2015 15:49
by Horschte
I've got the same problem, and I think a map is not what I need.

I have a few textfields in my widget named price_1, price_2 and so on. And I have variables in my flow named price_1, price_2 and so on.

I want to use this function to change the textfields with the values of the variables:

Code: Select all

setWidetElementProperty("Widget1", "price_1", "text", "{price_1}");
setWidetElementProperty("Widget1", "price_2", "text", "{price_2}");
This works fine but is a lot of writing.

So I want to do this in a for-loop.
It works with the second argument. But I don't know what to use in the fourth element:

Code: Select all

for(){...
setWidetElementProperty("Widget1", "price_{i}", "text", "{price_i}");
}
I tried a lot but nothings works for me. Any ideas?

Thanks. :-)

Re: Can we use dynamic variable names?

Posted: 07 Oct 2015 18:11
by Martin
You could use function getValue to get the value of a variable by name:

Code: Select all

setWidetElementProperty("Widget1", "price_{i}", "text", getValue("price_{i}", "---")); // "---" is the default value when the variable does not exist or is null
You could also use eval:

Code: Select all

setWidetElementProperty("Widget1", "price_{i}", "text", eval("price_{i}"));
Regards,
Martin

Re: Can we use dynamic variable names?

Posted: 07 Oct 2015 18:17
by Horschte
Thank you very much for your help.