Set variable value whose name is defined in another variable

3,236 views
Skip to first unread message

joaomgcd

unread,
Feb 17, 2013, 4:56:56 PM2/17/13
to tas...@googlegroups.com
Is something like this possible?

I want to set %TEST to 1

I have  the variable %par with the value %TEST

I would like to do something like set %%par to 1 (which would look inside %par for a name of a variable and in this case find %TEST and set that to 1), but that doesn't seem to work.

Any valid syntax to do this?

Thanks in advance.

mpizgatti

unread,
Feb 17, 2013, 5:50:40 PM2/17/13
to tas...@googlegroups.com
This is EXACTLY what I need to do. Any way to accomplish this?

In my case I'm trying to convert a date abbreviated FEB to 02 but just can't seem to call the variable changing it to 02 even though that variable IS NAMED FEB. Both are variables.

I'm not sure what all you've tried joaomgcd, I've tried %%par like you have. I've tried %(%par) and still no luck. I've checked and unchecked do maths...I'm at a frustratingly blocked road.

mpizgatti

unread,
Feb 17, 2013, 6:29:09 PM2/17/13
to tas...@googlegroups.com
Not sure exactly what you are trying to do. Now that I look at it's actually different from what I need. What is preventing you from just setting %TEST to 1?

João Dias

unread,
Feb 17, 2013, 6:34:06 PM2/17/13
to tas...@googlegroups.com
What I'm actually trying to do is set a Tasker variable with AutoRemote. 

Not sure if you're familiar with AutoRemote, but you can send commands remotely with the format "a b=:=c" and in tasker you would get %arpar1 as "a", %arpar2 as "b" and %arcomm as "c".

So, I wanted to send something like "setvar %TEST=:=1" and have Tasker set the variable %TEST to 1. It's just that the variable name %TEST is inside the %arpar2 variable in this case... So I was wondering if there was any special syntax for this situation. :)

--
You received this message because you are subscribed to the Google Groups "Tasker" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tasker+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Rich D

unread,
Feb 17, 2013, 6:49:54 PM2/17/13
to tas...@googlegroups.com

LOL... I actually understood joaomgcd's post (sorry I have no clue as to the answer ) but was curious as to exactly what you are trying to achieve?  :)

> In my case I'm trying to convert a date abbreviated FEB to 02 but just can't seem to call the variable changing it to 02 even though that variable IS NAMED FEB. Both are variables.

Rich....

Michael Stevens

unread,
Feb 17, 2013, 7:33:57 PM2/17/13
to tas...@googlegroups.com
I seem to recall (But can't find in the user guide on tasker wiki just now) that you can escape variable names using the "\" or "\\" (no quotes) so you can set variable names to variable names (or something along those lines) 
I'm not certain if this is what you're looking for though.

joaomgcd

unread,
Feb 18, 2013, 3:40:41 AM2/18/13
to tas...@googlegroups.com
Thanks, but that's not it unfortunately. I want to set a variable's value whose name I only have at "runtime" so to speak. That name is defined in another variable.
This is a case where you would use reflection, were this Java or C#. :)

TomL

unread,
Feb 18, 2013, 5:31:03 AM2/18/13
to tas...@googlegroups.com
use an array.

Tom

João Dias

unread,
Feb 18, 2013, 5:35:01 AM2/18/13
to tas...@googlegroups.com
How so Tom?

On Mon, Feb 18, 2013 at 10:31 AM, TomL <toml...@gmail.com> wrote:
use an array.

Tom

TomL

unread,
Feb 18, 2013, 6:18:19 AM2/18/13
to tas...@googlegroups.com
set %Month(1)="jan"
set %Month(2)="feb"

set %thismonthnum=2
alert: " %thismonthnum -> %Month(%thismonthnum) "

... or the reverse ...

set %thismonthname="feb"
alert: " %thismonthname -> %Month(#?%thismonthname) "

João Dias

unread,
Feb 18, 2013, 6:26:40 AM2/18/13
to tas...@googlegroups.com
Sorry, my mistake. I gave a bad example of what I wanted to do. Your example works but just for numbers, right?

What if I wanted to set %TEST to "abc" (instead of 1) where the name %TEST is contained in the %par variable?

TomL

unread,
Feb 18, 2013, 6:45:56 AM2/18/13
to tas...@googlegroups.com
%MyVarName(1)="one"
%MyVarName(2)="two"

%MyVarValue(1)="uno"
%MyVarValue(2)="dos"

perform task: indirection(%par1,%par2)
set %index=%MyVarName(#?%par1)
set %MyVarValue(%index)=%par2

usage example:
set %this="two"
notify: "changing value of var %this from %MyVarValue(#?%this), this was dos ... "
perform task: indirection(%this,"deux"), priority:10
notify: " ... to %MyVarValue(#?%this), this should be deux now."

Tom

João Dias

unread,
Feb 18, 2013, 6:47:29 AM2/18/13
to tas...@googlegroups.com
Nice one!
Good idea. :)

Thank you very much!

Jason Dwight

unread,
Sep 28, 2013, 2:51:59 PM9/28/13
to tas...@googlegroups.com
Im kind of late to the party on this one, but there's a much simpler way to do this.

From one device send a message like this:

SETVAR TEST=:= Testing

(arpar1) (arpar2)     (arcomm)
The first one is just a trigger so the device knows it's setting a variable.
The second one is the variable name you want to change, very specifically without the % that tasker uses.
The third is the value you want to set the variable to.

From the receiving device have this:

IF arpar1 ~ SETVAR

Variable Set %temp to %arpar2

JavaScriptlet
  Code: setGlobal(temp, arcomm)

END IF

Setting a temporary variable is neccesary, for some reason JavaScriptlet doesnt like arpar2.

Jens Hoofs

unread,
Oct 17, 2013, 1:13:27 AM10/17/13
to tas...@googlegroups.com
Could you explain a little more in detail what you exactly do there. Im trying to do something somewhat similair. Im trying to create a voice command with autovoice. That allows me to add new enterys. So i will have autovoice first look for %Menu wich contains Eventghost|Tablet|Phone than i want to be able to get in the menu of one of those. So i say Eventghost than i have %Voicecommand Eventghost. but now i want to be able to use %Eventghost out of that how do i get this to work ?

Op zaterdag 28 september 2013 20:51:59 UTC+2 schreef Jason Dwight:

Noah Gregory

unread,
Jan 31, 2014, 11:05:18 PM1/31/14
to tas...@googlegroups.com
If you still need help, I can tell you how to do it.

Pent

unread,
Feb 1, 2014, 2:24:12 AM2/1/14
to tas...@googlegroups.com
Since it's resurrected: you can use %%var from next version.

Variable Set, %name, colour
Variable Set, %%name, red

Sets %colour to red.

Pent

Noah Gregory

unread,
Feb 1, 2014, 11:42:15 AM2/1/14
to tas...@googlegroups.com

What do you mean "from the next version". Can you reword what you wrote?

--
You received this message because you are subscribed to a topic in the Google Groups "Tasker" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/tasker/kL7LR7-OJbE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to tasker+un...@googlegroups.com.
Visit this group at http://groups.google.com/group/tasker.

Pent

unread,
Feb 1, 2014, 11:47:01 AM2/1/14
to tas...@googlegroups.com

What do you mean "from the next version". Can you reword what you wrote?


It is in the next version that will come out.

Pent

Noah Gregory

unread,
Feb 1, 2014, 11:54:08 AM2/1/14
to tas...@googlegroups.com

OK. Another way to do it is with JavaScriptlet. setGlobal("%" + %TEST, "test"). Or something like that.

--

David Wang

unread,
May 26, 2014, 4:52:00 AM5/26/14
to tas...@googlegroups.com
But, how to make the content of a variable with prefix "%"  not to become a  variable reference? I couldn't see any related description in UserGuide.

Here is a situation I got:
1.Variable Set: %search = car
2.Run Shell:
sqlite3 xxx/Taske/sales.db;
select sale from Guangzhou like '%%search%';
.exit
I got nothing from the output, while I think it should output the result. I think Tasker consider "%%serach" as a variable reference. But I couldn't find any way to make the "%search" escape it.

The way I need: %%search%;
The way Tasker think: %%search%.

Pent

unread,
May 26, 2014, 5:00:58 AM5/26/14
to tas...@googlegroups.com


But, how to make the content of a variable with prefix "%"  not to become a  variable reference? I couldn't see any related description in UserGuide.

See 'Escaping Variable Names' in the Variables section of the userguide.

Pent 

David Wang

unread,
May 26, 2014, 7:12:56 AM5/26/14
to tas...@googlegroups.com
See 'Escaping Variable Names' in the Variables section of the userguide.

Pent 

Oh, I see. I must be make a mistake in the former example. In fact, I use non-English chars as the value of %search. 

And I found the real problem is %%search% will not be the value of %search with "%" prefix and suffix when the value of %search only contains non-English chars. Here I will show you how and you can reproduce it.

1.Variable Set: %search=car;
2.Flash %%search%
It will flash %car%. All right.

1.Variable Set: %search=car汽车;
2.Flash %%search%
It will flash %car汽车%. No problem.

1.Variable Set: %search=汽车car;
2.Flash %%search%
It will flash %汽车car%. No problem.

1.Variable Set: %search=汽车;
2.Flash %%search%
It will flash %%search%. Not the Chinese words——"汽车"!


So I think maybe it's a bug or limitation for Tasker.  

BTW: I met a strange phenomenon just now. I post the reply, but I couldn't see my thread in my web browser. So I have to write it down again. If you received more than one reply, just ignore the former ones. Sorry.

David Wang

unread,
May 27, 2014, 8:51:30 AM5/27/14
to tas...@googlegroups.com
And I found the real problem is %%search% will not be the value of %search with "%" prefix and suffix when the value of %search only contains non-English chars. Here I will show you how and you can reproduce it.

1.Variable Set: %search=car;
2.Flash %%search%
It will flash %car%. All right.

1.Variable Set: %search=car汽车;
2.Flash %%search%
It will flash %car汽车%. No problem.

1.Variable Set: %search=汽车car;
2.Flash %%search%
It will flash %汽车car%. No problem.

1.Variable Set: %search=汽车;
2.Flash %%search%
It will flash %%search%. Not the Chinese words——"汽车"!


So I think maybe it's a bug or limitation for Tasker.  


Can you reproduce it?



Pent

unread,
May 28, 2014, 8:44:07 AM5/28/14
to tas...@googlegroups.com

1.Variable Set: %search=汽车;
2.Flash %%search%
It will flash %%search%. Not the Chinese words——"汽车"!

That's correct. In every case, it's finding the variable reference %%search
in the Flash action (the last % is ignored because it's not followed by a valid variable name).

%%search means 'the variable whose name is held in %search'. In the
first 3 cases, that's a valid variable name, so the replacement is done.
In the last case, it's not a valid variable name, because variable names
have to have minimally 3 characters (yours only had 2)

Pent




David Wang

unread,
May 28, 2014, 7:54:48 PM5/28/14
to tas...@googlegroups.com
In the last case, it's not a valid variable name, because variable names
have to have minimally 3 characters (yours only had 2)

I see. I tried 3 Chinese characters, it's OK now.

1.Variable Set: %search=汽车;
2.Flash %%search%
It will flash %汽车%. No problem.

Because I want to run sqlite statements in Tasker. Some statements just like the one below: 
select sale from Guangzhou like '%%search%';
The value of %search is set via input.So the content of %search maybe contains less than 3 chars. The first "%" in former statement means SQL specific match pattern. In order to make effect, I use the following way. And it turned out to be OK. I write down the solution here so that someone may find it useful when they have the same problem.

1.Variable Set: %search=汽车;
2.Variable Set: %percent=%;
2.Flash %percent%search%
It will flash %汽车%. No problem.

Thanks for your reply.

Jens Hoofs

unread,
Jun 12, 2014, 3:54:58 PM6/12/14
to tas...@googlegroups.com
Is there a guide or tutorial on the %%Variable does it work with array's? Can i use the push pop and search with it ?
Is there any place where i can find its functionalities.



--
You received this message because you are subscribed to a topic in the Google Groups "Tasker" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/tasker/kL7LR7-OJbE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to tasker+un...@googlegroups.com.
Visit this group at http://groups.google.com/group/tasker.
For more options, visit https://groups.google.com/d/optout.

Jens Hoofs

unread,
Jun 12, 2014, 4:43:21 PM6/12/14
to tas...@googlegroups.com
It seems like only: Variable set and Variable split will work with the double %% i tested it and no other variable commands will work with it.

Pent

unread,
Jun 13, 2014, 2:20:52 AM6/13/14
to tas...@googlegroups.com
Can you give me an example of another action you tried it with ?

Should work most places.

Pent
To unsubscribe from this group and all its topics, send an email to tasker+unsubscribe@googlegroups.com.

Jens Hoofs

unread,
Jun 16, 2014, 10:04:58 AM6/16/14
to tas...@googlegroups.com
The following example wont work:

Set %Menuc - Mediaplayer
Set %%Menuc - test

that wont set %Mediaplayer to test
it will only work if the variable %Mediaplayer is already in the list of variables. So only when it is used somewhere else.



To unsubscribe from this group and all its topics, send an email to tasker+un...@googlegroups.com.

Pent

unread,
Jun 16, 2014, 10:45:05 AM6/16/14
to tas...@googlegroups.com


The following example wont work:

Set %Menuc - Mediaplayer
Set %%Menuc - test


I tried that with the current beta and it worked fine.

If you're using the current Play Store version, perhaps it's something I've fixed in the beta.

In any case, it's should definitely work with the next stable Tasker update.

Pent

RudeboyX

unread,
Jun 17, 2014, 5:45:24 AM6/17/14
to tas...@googlegroups.com
I'm still using the latest playtore version 4.3u4 and this is working fine for me.

RBX
To unsubscribe from this group and all its topics, send an email to tasker+un...@googlegroups.com.
Visit this group at http://groups.google.com/group/tasker.
For more options, visit https://groups.google.com/d/optout.

RudeboyX

unread,
Jun 17, 2014, 5:53:07 AM6/17/14
to tas...@googlegroups.com
more specifically im doing

set variable: %variabletype -> "Device"
set variable: %variablename -> "HZ%variabletype"
set: %%variablename -> "Phone"

(This sets %HZDevice to "Phone")

All the dynamic variables I use are local (lowercase). Maybe your issue only happens when you are using global variabls (at least 1 uppercase letter)

Regards
RBX

Jens Hoofs

unread,
Jun 17, 2014, 10:06:32 AM6/17/14
to tas...@googlegroups.com
Yes i see what you do here that does work for me too.
It only works if %Mediaplayer is set before it is used in a double  %%

So in my example:
Set %Menuc - Mediaplayer
Set %%Menuc - test
that wont set %Mediaplayer to test

But if i do the folowing:
Set %Mediaplayer - 0
Set %Menuc - Mediaplayer
Set %%Menuc - test

That will set %Mediaplayer to test. But what i am trying to do is for the double %% to create the variable %Mediaplayer with the content test


--

Bob Hansen

unread,
Jun 17, 2014, 10:12:04 AM6/17/14
to tas...@googlegroups.com
I believe that this is fixed in the latest beta version of Tasker as this does work on my phone.
Set %Menuc - Mediaplayer
Set %%Menuc - test

Does set %Mediaplayer to test

Jens Hoofs

unread,
Jun 17, 2014, 10:15:39 AM6/17/14
to tas...@googlegroups.com
I just tested the above example of Bob and that worked on my phone wich is really weird because it didnt work earlier on my tablet.

And i did check if there was an update for tasker. Are there different versions for tablet and phones?

Jens Hoofs

unread,
Jun 17, 2014, 10:37:21 AM6/17/14
to tas...@googlegroups.com
I found what is causing the problem. The example works but the variable wont be set in the variables list.
in my example doing:
Set %Menuc - Mediaplayer
Set %%Menuc - test
that wont set %Mediaplayer to test in the variables list.
But as soon as i try to see what it contains by doing Flash - %Mediaplayer
Than %Mediaplayer is set to test.
If i dont use the %Mediaplayer anywhere it wont show up in the variables list

Paul Vanderperren

unread,
Jun 17, 2014, 11:41:22 AM6/17/14
to tas...@googlegroups.com
I can confirm this. Global %% variables do not show up in the Vars list unless explicitly referenced elsewhere (eg 'flashing' them).
 But what is worse : Actions like Array Pop do NOT work on them :-(

Pent

unread,
Jun 18, 2014, 2:13:55 AM6/18/14
to tas...@googlegroups.com


I found what is causing the problem. The example works but the variable wont be set in the variables list.

Sorry, I can't reproduce that either, maybe something else fixed with the beta.

Pent

Pent

unread,
Jun 18, 2014, 2:32:33 AM6/18/14
to tas...@googlegroups.com

 But what is worse : Actions like Array Pop do NOT work on them :-(

Thanks, fixed for next update.

Pent

Paul Vanderperren

unread,
Jun 18, 2014, 4:24:38 AM6/18/14
to tas...@googlegroups.com
I doubt it - because I'm running the latest beta ...

Pent

unread,
Jun 18, 2014, 4:57:15 AM6/18/14
to tas...@googlegroups.com


I doubt it - because I'm running the latest beta ...

The latest beta is only on my laptop,  the 'next update' is b6.

Pent

Paul Vanderperren

unread,
Jun 18, 2014, 10:10:27 AM6/18/14
to tas...@googlegroups.com
May I add 'Variable Join' to the list of actions that do not work with redirected variables ?
Next update/beta ;-)  ?

Pent

unread,
Jun 18, 2014, 10:38:03 AM6/18/14
to tas...@googlegroups.com

May I add 'Variable Join' to the list of actions that do not work with redirected variables ?

Sure, I'm happy to fix those cases immediately. As you can probably tell, %% variable references were
added  relatively recently :-)

Next update/beta ;-)  ?

Yep.

Pent

Paul Vanderperren

unread,
Jun 18, 2014, 12:54:40 PM6/18/14
to tas...@googlegroups.com
Thx Pent.
I'm afraid I'm gearing up to pester you with bug reports, suggestions, requests, ...
All for the good cause, of course - and just making sure you still have one or two things on your todo list :-)
Anyway, just wanted to say I consider Tasker a work of genius - kudos to you !

Pent

unread,
Jun 18, 2014, 2:14:28 PM6/18/14
to tas...@googlegroups.com

I'm afraid I'm gearing up to pester you with bug reports, suggestions, requests, ...

Try to pace them!

>Anyway, just wanted to say I consider Tasker a work of genius - kudos to you !

Well, if you say so :-)

Pent

Jens Hoofs

unread,
Jun 21, 2014, 3:38:22 PM6/21/14
to tas...@googlegroups.com
Could someone with the beta test the read file and write file with the double %% in both the path from wich to write and read and to wich variable with %%. That is what i will be using and from the latest google play version it doenst work yet.

SmokeNMirrors

unread,
Dec 19, 2014, 4:31:27 PM12/19/14
to tas...@googlegroups.com
Sorry to bring up this older topic but it's the most comprehensive discussion of %%var usage I can find.

My question is what happens to memory if %var holds a global name (starts with upper case) and is never directly referenced?

So if I do
   Set %counter = %counter +1 (do math)
   Set %var = MYGLOBAL%counter
   Set %%var = MYVALUE
repeatedly

Then MYGLOBAL1, MYGLOBAL2... etc are created as global variables but do not appear in the variables list unless referenced directly.  

Will these live forever?  Is this leaking memory?  How do I free/release them after one or more profile/tasks process them?

Is there some kind of garbage collection?  If I want to free them then, from what I read, I need to uninstall/reinstall Tasker entirely!???  

I suppose I could do something like 
Set %counter = (%counter +1) % %rollover (do maths - the lone '%' is the modulus operator)
and choose %rollover to be large enough to handle sufficient simultaneous instances and limit the variable names created.

Pent

unread,
Dec 20, 2014, 3:50:39 AM12/20/14
to tas...@googlegroups.com
>Then MYGLOBAL1, MYGLOBAL2... etc are created as global variables but do not appear in the variables list unless >referenced directly. 

They should be there somewhere, they are not different to variables created in any other way.
Check you have 'indexed' checked in the vars list and that you're in the base project
(where unreferenced vars appear)



Will these live forever?  Is this leaking memory?  How do I free/release them after one or more profile/tasks process them?

You can use Variable Clear.

Pent

SmokeNMirrors

unread,
Dec 22, 2014, 1:08:41 PM12/22/14
to tas...@googlegroups.com
Thanks very much Pent.  Found them in my Home profile (sorry - didn't know to look there).  They vanish after clearing them.  I'll have my tasks cleanup as they go.

kalhimeo

unread,
Sep 18, 2015, 5:03:21 AM9/18/15
to Tasker
Hi Pent,

Sorry to bring up this old topic, but it looks like some Actions do not work when using redirected variable as an array name.

So far I found that a simple action like 'Variable Set' :

A1: Variable Set [ Name:%array_name To:search_me Do Maths:Off Append:Off ]
A2: Variable Set [ Name:%%array_name(1) To:one Do Maths:Off Append:Off ]

or 'Variable Search Replace' :

A1: Variable Set [ Name:%array_name To:search_me Do Maths:Off Append:Off ]

A2: Array Push [ Name:%%array_name Position:1 Value:one Fill Spaces:Off ]

A3: Variable Search Replace [ Variable:%%array_name(1) Search:one Ignore Case:Off Multi-Line:Off One Match Only:Off Store Matches In: Replace Matches:On Replace With:two ]


... will bring up an error.

The 'Variable Set' problem can easily be avoided by using 'Array Push' which works fine with redirected variables as array name (there are some other actions which support it too, even %%array_name(#) will work), but it's a bit more complex for 'Variable Search Replace'.


I realize that this kind of use looks a bit weird, but it can be useful to create tasks acting on different arrays.


Keep up the good work, Tasker is an amazing tool for Android.


Best regards,

Laurent


Alex Ceccon

unread,
Oct 16, 2016, 10:56:52 PM10/16/16
to Tasker

Hi Guys,

I finishing here looking for a method to name a variable with other variable name…

This way was how I solved:

I needed these variables to compound the final variable name:

%light = 2

%info = on

…and the final var would be: %Light2_on.


It is impossible to reach at once with something like: %Light%light_%info.

But doing few steps before its works!


Set var: %varname = Light%light_%info returns: Light2_on

Set var: %%varname = (anything) ---> actually creates the variable: %Light2_on

Alex Dannie

unread,
Oct 8, 2017, 10:03:48 PM10/8/17
to Tasker
Hi Pent

Sorry to bother, but if I set PosX variable for 100, which is position of object in the Content of some button I created manually, and I set PosX before his creation, then inside the content instead of 100 I can write just ,PosX,100 right?

I mean, if button is set to 100,100 in content and I replace it with PosX,PosY then it will not work, why?

Should I add %PosX,%PosY since its nested?

I wanted to create in a loop 5 buttons using predefined before x and y positions.

Can You advise me, because no one knows how or all know and I misunderstood Java. I use 4.8u4

Reply all
Reply to author
Forward
0 new messages