Can we use dynamic variable names?

Post your questions and help other users.

Moderator: Martin

Post Reply
natong
Posts: 80
Joined: 29 Aug 2015 05:24

Can we use dynamic variable names?

Post by natong » 02 Sep 2015 02:44

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);

User avatar
kintrupf
Posts: 257
Joined: 10 Sep 2013 08:59

Re: Can we use dynamic variable names?

Post by kintrupf » 02 Sep 2015 05:11

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);
...

Horschte
Posts: 56
Joined: 03 Nov 2014 18:00

Re: Can we use dynamic variable names?

Post by Horschte » 06 Oct 2015 15:49

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. :-)

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Can we use dynamic variable names?

Post by Martin » 07 Oct 2015 18:11

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

Horschte
Posts: 56
Joined: 03 Nov 2014 18:00

Re: Can we use dynamic variable names?

Post by Horschte » 07 Oct 2015 18:17

Thank you very much for your help.

Post Reply