Accessing dictionary value via variable key

sousben

New member
Hi,
Say I have the following:
Dictionary prices = {'s': 30, 'm': 35, 'l': 38 ...}

Then during execution I do set variable
String userSelection = 's'

Next I am trying to set variable
userPrice = {lv=prices[{lv=userSelection}]}

The UI doesnt even let me validate this expression.
When I do
userPrice = {lv=prices}
Then this is an acceptable expression.

How can I get a dictionary value from a variable key?
 

FrameXX

Well-known member
It seems to work for me.

I created dictionary named prices with valzes as you described

I created stribg variable named userSelection and set it's value to "s" for example.

I create integer variable called userPrice.

I have created set variable action for userPrice and used expression: {lv=prices[{lv=userSelection}]}.

I was allowed to press ok and userPrice was set to 30 after I tested the action.

Note that when writing the expression I needed to write {lv=userSelection} manually into square brackets. It wouldn't show in magic text list, because I was setting integer variable and MacroDroid didn't want me to use strings.

What version of MacroDroid are you using? I know there have been some fixes around this problem lately.
 
Last edited:

Endercraft

Moderator (& bug finder :D)
Note that when writing the expression I needed to write {lv=userSelection} manually into square brackets. It wouldn't show in magic text list, because I was setting integer variable and MacroDroid didn't want me to use strings.
I believe that's why strval was added.
 

sousben

New member
It seems to work for me.

I created dictionary named prices with valzes as you described

I created stribg variable named userSelection and set it's value to "s" for example.

I create integer variable called userPrice.

I have created set variable action for userPrice and used expression: {lv=prices[{lv=userSelection}]}.

I was allowed to press ok and userPrice was set to 30 after I tested the action.

Note that when writing the expression I needed to write {lv=userSelection} manually into square brackets. It wouldn't show in magic text list, because I was setting integer variable and MacroDroid didn't want me to use strings.

What version of MacroDroid are you using? I know there have been some fixes around this problem lately.
Ok thanks for testing.
This seems to be working because the user selection variable was created as an input variable with an acceptable value.

If you let that variable empty (user prompted) then it would probably not work (this is the behaviour I observe).

My desired behaviour is a variable that gets its value at runtime, but this example helps me: it's possible to assign the variable an acceptable value, create the set variable action, then just delete the static content of user_selection.
This works fine for me and accepts the user input value.

I didnt need the strvalue in this case...
 

FrameXX

Well-known member
If you let that variable empty (user prompted) then it would probably not work (this is the behaviour I observe).

This is happening because when you are creating the expression for integer variable MacroDroid observes and evaluates the expression on every change and checks if the result is a number. If the userSelection is empty or it is set to value that isn't any of the keys in prices dictionary the expression won't be evaluated properly and for example if userSelection would be an empty string this: {lv=prices[]} would be returned after evaluation. Since none of the keys in prices dictionary is an empty string no value will be returned and the expression will be left as it is. Since this output is not a number MacroDroid won't let you press OK.
 

sousben

New member
This is happening because when you are creating the expression for integer variable MacroDroid observes and evaluates the expression on every change and checks if the result is a number. If the userSelection is empty or it is set to value that isn't any of the keys in prices dictionary the expression won't be evaluated properly and for example if userSelection would be an empty string this: {lv=prices[]} would be returned after evaluation. Since none of the keys in prices dictionary is an empty string no value will be returned and the expression will be left as it is. Since this output is not a number MacroDroid won't let you press OK.
Yes I get that now, thanks to you.
You've unstuck me 😃
 
Top