Find position in an array (indexOf)?

745 views
Skip to first unread message

Mike L

unread,
Jun 28, 2013, 12:01:45 PM6/28/13
to tas...@googlegroups.com
So I am trying to find the index of something in an array. Tasker doesn't have an action for this as far as I can tell, so I am trying to use a Javascriptlet to do this.

Test4 (111)
A1: Array Push [ Name:%arr Position:10 Value:apple Fill Spaces:Off ]
A2: Array Push [ Name:%arr Position:10 Value:banana Fill Spaces:Off ]
A3: Array Push [ Name:%arr Position:10 Value:kiwi Fill Spaces:Off ]
A4: Variable Set [ Name:%arr To:%arr() Do Maths:Off Append:Off ]
A5: Variable Set [ Name:%find To:kiwi Do Maths:Off Append:Off ]
A6: JavaScriptlet [ Code:var arr=[arr];
var position = arr.indexOf(find); Libraries: Auto Exit:On Timeout (Seconds):10 ]
A7: Flash [ Text:%position Long:Off ]

This is my test profile, but %position always contains -1
Any help appreciated


Rich D

unread,
Jun 28, 2013, 12:18:38 PM6/28/13
to tas...@googlegroups.com

> So I am trying to find the index of something in an array. Tasker doesn't have an action for this as far as I can tell, so I am trying to use a Javascriptlet to do this.

Many have missed this one... from the guide..

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:

(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)

Mike L

unread,
Jun 28, 2013, 12:37:06 PM6/28/13
to tas...@googlegroups.com
Damn I thought I had that thing memorized by now. Tasker is always two steps ahead of me. Thanks man.

Mike L

unread,
Sep 29, 2013, 11:46:19 PM9/29/13
to tas...@googlegroups.com
Okay so I have another question about this. In Tasker, using indexOf() behaves weird. It won't accept an array, but it will search through a string treating it like an array.

A1: Variable Set [ Name:%array To:1,2,3,4,5 Do Maths:Off Append:Off ] 
A2: Variable Split [ Name:%array Splitter:, Delete Base:Off ] 
A3: [X] Variable Clear [ Name:%array Pattern Matching:Off ] 
A4: JavaScriptlet [ Code:var index = array.indexOf(3); Libraries:https://ajax.googleapis.com/ajax/libs/mootools/1.4.5/mootools-yui-compressed.js Auto Exit:On Timeout (Seconds):45 ] 
A5: Flash [ Text:%index Long:Off ] 

Leaving in the variable clear will make this always return -1. This task as is flashes 5, while I am expecting 2. 
Deleting the commas in A1 and powering off/leaving out A2 will make this flash 2. 
Deleting the commas in A1 and using array.indexOf(12) will flash 0.
I've tried all of this with the Moo Tools library included and without. I'm new to javascript so I'm hoping this is just a syntax issue, but this same coding seems to work on jsfiddle.net which is where I've been testing everything.



Pent

unread,
Sep 30, 2013, 7:48:59 AM9/30/13
to tas...@googlegroups.com
All Tasker variable (and array) values are strings, and are also imported
into JS as so '1', '2', '3', '4' and '5' in this case, so you would need to
do indexOf( '3' ), the right answer being 2 (since JS arrays start at 0).

Whether it should import things that look like integers as integers (boolean
has boolean etc), not sure, but it would break things for many people right now, so won't be
doing it I think.

Secondly, if you have

%arr, %arr1, %arr2 etc defined, Tasker isn't sure whether to import
%arr as a simple variable or an array. If in doubt, delete the ones
that don't apply first (%arr in this case).

Pent

Mike L

unread,
Sep 30, 2013, 4:25:41 PM9/30/13
to tas...@googlegroups.com
The quotes fixed it, thanks man. 
Putting just a string instead of an array through indexOf() will make the script return nothing when using an online editor. Odd that Tasker will accept it
Reply all
Reply to author
Forward
0 new messages