Tasker Tip Tuesdays

Tasker Tip Tuesdays: How To Use Special Variable Array Syntax

Tasker Tip Tuesdays - How To Use Special Variable Array Syntax

Welcome to Tasker Tip Tuesdays​, quick tips to help grow your Tasker knowledge.  This week I discuss how to use special variable array syntax.  If you use tasker arrays often you’ll love this useful tip.

 

Variable Arrays




If you have been around Tasker long enough, you have come across a situation when you need to use variable arrays.  Whether it is in a For Loop, or you’ve used the List Files Action, knowing how to manipulate arrays is a must.  This is especially true if you want to obtain a deeper knowledge of Tasker.

Special Variable Array Syntax

Variable arrays are what Tasker refers to as arrays.  While not quite a true array, as other programming languages use, it does support something similar.  Pent describes them as “pseudo-arrays”.  These pseudo-arrays are basically values “just stored in Tasker in index’d positions” as stated by Reddit user /u/Rachet_Guy.  Well said Rachet!  They contain indexed elements and a separator.  Often referenced as “%arr()” with parentheses while each element by using its index “%arr1” or “%arr(1)”.  But did you know there are special characters available to reference specific elements or element values?  See the Variable Arrays user guide excerpt below:

Examples

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. Here are some examples:

  • %arr(#)
    The number of defined array elements (4 in this case)
  • %arr(#>)
    The index of the first defined array element, or 0 if none are defined (1).
  • %arr(#<)
    The index of the last defined array element, or 0 if none are defined (4)
  • %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)
  • %arr(>)
    The contents of the first defined array element (a)
  • %arr(<)
    The contents of the last defined array element (d)
  • %arr() or %arr(:)
    All of the array elements separated by commas (a,b,c,d)
  • %arr(2) or just %arr2
    The content of the element with index 2 (b)
  • %arr(2:4)
    Contents of defined elements with indices 2 to 4 (b,c,d)
  • %arr(:3)
    All the defined elements with indices up to 3 (a,b,c)
  • %arr(3:)
    All the defined elements with indices starting from 3 (c,d)
  • %arr(1:2)
    All the defined elements with indices from 1 to 2 (a,b)

Notes:

  • arrays will virtually always have all their elements defined so e.g. %arr(>) will be the same as %arr(1), %arr(#) will be the same as %arr(#<)
  • index specifiers can themselves be variables (e.g. %arr(1:%MAX) or %arr(#?%FINDME)) but not variable arrays

Examples

Now that we know about the special variable array syntax let’s play around with some examples to get a better understanding.  In the video below, I gave examples of both simple and advanced syntax. If you have been following this series, you will also notice I used a bit of variable maths too!


Further Reading

To catch up on previous posts in this series, visit the Tasker Tip Tuesdays page.  As always, enjoy and keep learning!  Until the next Tasker Tip Tuesday…

2 thoughts on “Tasker Tip Tuesdays: How To Use Special Variable Array Syntax

  1. Very useful overview, thanks a lot for this!
    I never knew there was this syntax: %arr(<) (The contents of the last defined array element)
    This alone should save me a few extra actions in my tasks!

What did you think???