for-loop: Using variable as item

2,659 views
Skip to first unread message

Charlie Hagerman

unread,
Oct 14, 2011, 4:36:51 AM10/14/11
to Tasker
Hi, I have an issue with the for-loops. Ill give you an example:

Query variable
%variable

For
%jonas
Item: 1:%variable
flash
%jonas
End for

So if I put the variable to lets say 5, I want the loop to iterate 5
times and for every iteration it will display %jonas. %jonas will go
from 1 to 5 in steps of one with every loop.

What happens if I run this task is that it flashes just once saying
'1:5'.

I'm guessing the problem less within 1:%variable. Can someone explain
to me how I can get this to work? :)

V Oz

unread,
Oct 14, 2011, 5:21:11 AM10/14/11
to tas...@googlegroups.com

In adition to simple text, the For action accepts any comma-separated combination of these Items:
a numeric range e.g. 1:5 (= 1,2,3,4,5)
a numeric range with a jump e.g. 8:2:-2 (= 8,6,4,2)
a variable name (which is replaced) e.g. %fruit (= banana maybe)
a variable array part e.g. %arr(1:2) (= %arr1, %arr2 = apple,banana maybe)

A common case is to use %arr(), which performs a loop for each element in the array %arr.


I think it does not accept numeric ranges with variables

Pent

unread,
Oct 14, 2011, 5:47:46 AM10/14/11
to Tasker

> I think it does not accept numeric ranges with variables

It does.

Have a look at the for loop example in Flow Control. And here are all
the
array index specifications:

http://tasker.dinglisch.net/userguide/en/variables.html#arrays

Pent

BossMan

unread,
Oct 14, 2011, 5:59:27 AM10/14/11
to tas...@googlegroups.com
But this is the case of arrays - what about an ordinary numeric range (i.e. not %arr(1:%var), but 1:%var)?

BR,
A.


BossMan

unread,
Oct 14, 2011, 6:13:56 AM10/14/11
to tas...@googlegroups.com
I know how to do it :-)

In your example, do first:
Array Push (%arr, 1, 1)
and then in your foreach loop do %arr(1:%variable) - it will flash 5 times.

BR,
A.

Charlie Hagerman

unread,
Oct 14, 2011, 6:50:30 AM10/14/11
to Tasker
could you specify that a bit?

I know have:

Array Push
%arr, Position 1, Value 1

Variable query
%variable

for
%jonas, items %arr(1:%variable)
flash
%jonas
end for

It flashes 5 times but like this; 1
%arr2 %arr3 %arr4 %arr5

I guess I have to fill upp the array with values, but my knowledge of
arrays is limited...could you help me with that aswell? :)

Pent

unread,
Oct 14, 2011, 8:13:42 AM10/14/11
to Tasker

> For
> %jonas
> Item: 1:%variable
>     flash
>     %jonas
> End for
>
> So if I put the variable to lets say 5, I want the loop to iterate 5
> times and for every iteration it will display %jonas. %jonas will go
> from 1 to 5 in steps of one with every loop.
>
> What happens if I run this task is that it flashes just once saying
> '1:5'.

Sorry, I missed the problem first time round.

A possible solution is using 1:9999999 for the range and in the loop
put a

Goto, Loop End, If %jonas = %variable

Pent

BossMan

unread,
Oct 14, 2011, 9:08:30 AM10/14/11
to tas...@googlegroups.com
You can use either Pent's solution or continuing the array path:

....
set %counter = 1

for
%jonas, items %arr(1:%variable)
     flash %counter
     set %counter = %counter + 1
end for

This should flash with 1, 2, 3, 4, 5.

BR,
A.

TomL

unread,
Oct 14, 2011, 9:21:03 AM10/14/11
to Tasker
Pent, is this supported in Tasker 1.1.1u2?

for %val 8:2:-2
flash %val
end for

When I try, it just does "8". and never goes to 6 4 2.

Tom



a numeric range with a jump e.g. 8:2:-2 (= 8,6,4,2)

On Oct 14, 5:21 am, V Oz <v.ozzzz...@gmail.com> wrote:
> In adition to simple text, the For action accepts any comma-separated
> combination of these Items:
> *a numeric range* e.g. 1:5 (= 1,2,3,4,5)
> a numeric range with a jump e.g. 8:2:-2 (= 8,6,4,2)
> *a variable name* (which is replaced) e.g. %fruit (= banana maybe)

Pent

unread,
Oct 14, 2011, 2:20:16 PM10/14/11
to Tasker
> Pent, is this supported in Tasker 1.1.1u2?
>
> for %val 8:2:-2
> flash %val
> end for

Should do. You maybe left spaces ? Try enabled Menu / Prefs / Misc /
Popup Errors/Warnings.

Pent

TomL

unread,
Oct 14, 2011, 2:45:35 PM10/14/11
to Tasker
Popup Errors/Warnings was already enabled, no warnings/errors. No
extra spaces.

Here's a summary of what I see.


for %val 2:5 flash %val end produces correct output: "2 3 4 5"
for %val 2:5:1 flash %val end produces correct output: "2 3 4 5"
for %val 2:5:-1 flash %val end produces *incorrect* output: "2 1 0
-1 ... " and so on

for %val 5:2 flash %val end produces correct output: "5"
for %val 5:2:-1 flash %val end produces *incorrect* output: "5"
for %val 5:2:1 flash %val end produces *incorrect* output: "5 6 7
8 ... " and so on

for %val 5 flash %val end produces correct output: "5"
for %val 5:5 flash %val end produces correct output: "5"
for %val 5:5:-1 flash %val end produces correct output: "5"
for %val 5:5:1 flash %val end produces *incorrect* output: "5 6 7
8 ... " and so on


Tom

Pent

unread,
Oct 15, 2011, 4:03:55 AM10/15/11
to Tasker
Thanks for the summary, I'll take a look before the next version comes
out.

> for %val 2:5:-1 flash %val end produces *incorrect* output: "2 1 0
> -1 ... " and so on

Well, at negative infinity it will switch to positive infinity and
eventually get to 5 I guess.

> for %val 5:2:1 flash %val end produces *incorrect* output: "5 6 7

As above except opposite :-)

Pent

V Oz

unread,
Feb 1, 2012, 2:41:19 AM2/1/12
to tas...@googlegroups.com
As I see the problem is still there ? :(

Pent

unread,
Feb 1, 2012, 3:45:31 AM2/1/12
to Tasker
> As I see the problem is still there ? :(

If you mean the original 'missing feature', I put it in a couple of
days ago.

Pent
Reply all
Reply to author
Forward
0 new messages