how can I push a variable to an array only if that particular variable doesn't exist

47 views
Skip to first unread message

Brett Owens

unread,
May 7, 2013, 1:29:53 AM5/7/13
to tas...@googlegroups.com
I'm finally getting the concept of arrays but I haven't figured out how to make it so a button I have only adds a new variable if it doesn't already exist in the the current array. Can anyone point me in the right direction? Thanks

Pent

unread,
May 7, 2013, 1:58:51 AM5/7/13
to Tasker
> I'm finally getting the concept of arrays but I haven't figured out how to make it so a button I have only adds >a new variable if it doesn't already exist in the the current array. Can anyone point me in the right direction?

I guess you mean 'if the value of the new variable isn't already in
the array'.

Try:

If %arrayName(#?%variable) Matches 0

Can't remember if you can have a variable name with #?.

Pent

Brett Owens

unread,
May 7, 2013, 9:24:16 AM5/7/13
to tas...@googlegroups.com
Hi, I couldn't get that to work. I also tried if %array(#?varx) doesn't match *varx* and no luck. What else can I try?

Matt R

unread,
May 7, 2013, 5:38:53 PM5/7/13
to tas...@googlegroups.com
Not sure if this applies to all version of Tasker, but on v1.6, doing if %array(#?keyword) ~ 0 causes a crash. Another couple options exist. You can do variable set %exists to %array(#?%var) and then do if %exists ~ 0. Or, you could do if %array() !~ *%var*, assuming you don't have to worry about %var being contained in one of your array elements (but not matching exactly, like "car" in "cartoon").

Matt

UncleMike

unread,
May 7, 2013, 5:53:50 PM5/7/13
to tas...@googlegroups.com
On Tuesday, May 7, 2013 1:58:51 AM UTC-4, Pent wrote:

If %arrayName(#?%variable) Matches 0

Can't remember if you can have a variable name with #?. 

I'm using it in my Notification Reminder profiles and it works fine:

A1: Variable Set [ Name:%idx To:%nrLBL(#?%par1) Do Maths:Off Append:Off ] 

It returns the element index if the value appears in the array, and zero otherwise. 

apps4...@gmail.com

unread,
May 7, 2013, 9:53:37 PM5/7/13
to tas...@googlegroups.com
Thank you everyone, it's working for me now.

Cptnodegard

unread,
May 7, 2013, 10:30:58 PM5/7/13
to tas...@googlegroups.com
What is #?
?

Some sort of wildcard?

Rich D

unread,
May 7, 2013, 10:42:57 PM5/7/13
to tas...@googlegroups.com

>
> What is #?

From the user guide -- variables--

If the four variables %arr1, %arr2, %arr3, %arr4 hold respectively a, b, c and d then we have an array with 4 elements. These variables can be used just like any other,however it is also possible to access them in special ways.

%arr(#?b/c) A comma-separated list of the array indices (lowest to highest) with matching values,or 0 if none match (2,3 in the example)

It is very useful but easily missed in the user guide.  As Matt pointed out it will throw a error when using a local array but it does work with global arrays or with Matts suggested work around.

Rich..

Cptnodegard

unread,
May 8, 2013, 1:39:37 AM5/8/13
to tas...@googlegroups.com
That is indeed useful, must have read that section half a dozen time and it never stuck. 
Reply all
Reply to author
Forward
0 new messages