Use a variable's value in the name of another variable?

Post your questions and help other users.

Moderator: Martin

Post Reply
moonspeak
Posts: 25
Joined: 15 Jul 2015 19:42

Use a variable's value in the name of another variable?

Post by moonspeak » 24 Jul 2017 20:01

So like the title says, is it possible to use the value of a variable in the name of another variable?

What I'm trying to do is record how many times a notification is received for individual conversations from several messaging apps. Essentially, I want to create a variable that would be named {global_notification_count{app_name}_{title}}, which, for example, would create a variable actually named something like {global_notification_count_WhatsApp_Group1} or {global_notification_count_Messenger_Some_Other_Group}. Obviously that nested bracket thing I wrote won't work, but is there some way to achieve this?

Also, I assume spaces in conversation titles would be problematic, so hopefully the solution, if any, can be wrapped in a replace function to replace spaces with underscores.

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Use a variable's value in the name of another variable?

Post by Desmanto » 25 Jul 2017 02:37

There is a way, similiar to indirect method in excel.
To declare the GloVar, use setValue() instead of usual direct equal sign. We cannot declare the value directly, since we want to replace it in inline. To refer to the GloVar, We need to use eval(). If we don't eval it, the variable will be assigned to variable name, not the content of the variable

Creating GloVar :
appname = "Whatsapp";
setValue("global_{appname}","something");

You will have new GloVar, global_whatsapp = "something"
Create as many as needed.

To refer to that GloVar dynamically :
appname = "whatsapp";
x = eval("global_{appname}");

This essentially the same as : x = {global_whatsapp}.

While actually, if no dynamic linking, x = global_whatsapp, will assigned x to the value of global_whatsapp. But if you don't use eval in dynamic linking, x = "global_{appname}", will assigned x to "global_whatsapp". Try this to see the difference.

appname = "whatsapp";
global_whatsapp = 25;
x = global_whatsapp;
y = eval("global_{appname}");
z = "global_{appname}";

The result :
x = 25
y = 25
z = "global_whatsapp"

The value will be dynamically replaced by whatever the {appname}. That line is for example, you probably get the {appname} value from the trigger already.

============================

You can add another {group_name} there, but I won't suggest you to do that. Imagine you have 4 messengers and have 5 groups each. You will have to create 20 GloVar which each contain single value. I hate scrolling when I use debug dialog, just because this long list of GloVar with only single value.

The better way to do it is to put each group into single messenger GloVar. So you will have global_whatsapp contain a list of notification group, example : global_whatsapp = (1, 4, 25, 0, 1). Then somewhere in the flow, create a mapping which will translate something like
0 = group A
1 = group B
...

When it is group A, then the notif will be saved to global_Whatsapp[0], group B to global_Whatsapp[1] etc. So based on the example, Group A have 1 notif, group B have 4 notifs, etc. You only need to create GloVar as many as your messenger.

Hopefully I am not confusing here. If it is easy to understand, I wanna add this to my own manual.

As for the spaces in notif title, i don't quite understand what you mean. You can use replaceAll to replace spaces into underscores.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

Post Reply