Return variable value and goto top/end of loop questions

603 views
Skip to first unread message

brandall

unread,
Jan 18, 2012, 1:42:05 PM1/18/12
to Tasker
Hi folks, I have the following structure:

[Task Name BLAR] Priority 5
Do things
Perform task Blar1 priority 9 return variable value %VAR
*I have more things to do here*

[Task Name BLAR1]
Do things
Perform task Blar2 priority 10 return variable value %VAR

[Task Name BLAR2]
Do things
Variable set %VAR to 1
Return variable value %VAR

The tasks are taking a while to run (although they do have some
complex actions in them) and I'm wondering if using the return
variable value in the way I am could be causing an issue?

Also:

IF
Something
IF
Something else
Goto top/end of loop
End IF
Something
End IF

In the above example (where I either use top or end), which IF or End
does it go to?

I did search in the user guide (honest! here is a broken link I found
to prove it http://tasker.dinglisch.net/userguide/en/actiongroup), but
no joy..

Thanks in advance

TomL

unread,
Jan 18, 2012, 1:52:46 PM1/18/12
to tas...@googlegroups.com
Why are you re-using global var %VAR as a task return value three separate times?  That's bound to cause confusion.  Use a local var like %var instead.

As a further suggestion, I found that I tended to overuse "perform tasks" to chain actions together, making for awkward and problematic code.  It's probably a holdover from my days of using BASIC that leads me to first try using "perform tasks" when there might be better ways of doing things.  Like this:

Action: Blar
1. do things 
2. set trigger Blar1
3. stop

Profile: Blar1
condition: watch for trigger  Blar1
actions: do things, set trigger Blar2, stop

Profile: Blar2
condition: watch for trigger  Blar2
actions: do things, set trigger Blar3, etc...

This way actions finish ASAP without needlessly hanging around waiting for sub-actions to finish.

Tom

brandall

unread,
Jan 18, 2012, 2:16:41 PM1/18/12
to Tasker
> Why are you re-using global var %VAR as a task return value three separate
> times?  That's bound to cause confusion.  Use a local var like %var instead.

%VAR was just an example to make myself clear (!). It's of course not
%VAR

> This way actions finish ASAP without needlessly hanging around waiting for
> sub-actions to finish.

I hear you, but this 'project' has 13 profiles, 46 tasks and 1000
actions that all intertwine, so switching to triggers is not an
option. Everything executed an intertwined in seconds prior to the
addition of the three tasks above... So, something is amiss in the
structure or the actions within it...

Pent

unread,
Jan 18, 2012, 2:33:57 PM1/18/12
to Tasker
> The tasks are taking a while to run (although they do have some
> complex actions in them) and I'm wondering if using the return
> variable value in the way I am could be causing an issue?

The Run Log next release should give you a lot of information.

> IF
> Something
> IF
> Something else
> Goto top/end of loop
> End IF
> Something
> End IF
>
> In the above example (where I either use top or end), which IF or End
> does it go to?

Neither, 'If' blocks aren't loops.

Pent

brandall

unread,
Jan 18, 2012, 3:06:57 PM1/18/12
to Tasker
> The Run Log next release should give you a lot of information.

Excellent!

> Neither, 'If' blocks aren't loops.

Oh my God......... A lot of editing lies ahead of me...

I've just spotted the LoopStart label in the guide....

Damn it...

brandall

unread,
Jan 18, 2012, 3:09:43 PM1/18/12
to Tasker
> Neither, 'If' blocks aren't loops.

Just to clarify, does it recognise the label 'LoopStart' and return to
there? Or am I misreading the example?

BossMan

unread,
Jan 18, 2012, 3:18:51 PM1/18/12
to tas...@googlegroups.com
Top of the loop/end of the loop are basically equivalents of continue/break keywords in programming languages, so they will work with loops ('for' loop in tasker).

BR,
A.

brandall

unread,
Jan 18, 2012, 4:10:48 PM1/18/12
to Tasker
> Top of the loop/end of the loop are basically equivalents of continue/break keywords in programming languages,  so they will work with loops ('for' loop in tasker).

Thanks... I wrongly assumed it would head back to the top of the IF,
where all of my loops start - IF something isn't set...etc etc...

Anyway, I'm glad you replied BossMan - I remember your array skills
from many moons ago and wanted to pick your brains about how I'm
structuring a task...

Fancy a challenge?

BossMan

unread,
Jan 19, 2012, 3:37:13 AM1/19/12
to tas...@googlegroups.com
Fancy a challenge?

Go ahead :) Though I can't promise anything as my free timeslots are pretty random :)

BR,
A. 

TomL

unread,
Jan 19, 2012, 7:44:11 AM1/19/12
to tas...@googlegroups.com
Out of curiousity, what is your project about? Is this the Android deep sleep / running process thing you were working on?

Tom

brandall

unread,
Jan 19, 2012, 1:52:17 PM1/19/12
to Tasker
> Go ahead :) Though I can't promise anything as my free timeslots are pretty
> random :)

Thank you - here you go!

I have a data set in the file dsdwado.txt that contains an active
application output such as the following:

net.dinglisch.android.taskerm
com.android.mms
/data/data/com.android.smith/bin/smithd
de.devmil.minimaltext
com.htc.android.htcime:provider
com.levelup.beautifulwidgets
com.google.android.apps.maps:NetworkLocationService
com.htc.bgp
com.facebook.katana
com.android.vending
com.google.android.apps.plus
com.google.android.apps.uploader
com.htc.android.mail
com.sdgtl.watch.flyer
com.htc.bg
bbc.iplayer.android
com.noshufou.android.su

I want Tasker to check each line using the 'test package name' action
and convert if from com.facebook.katana to Facebook for example so
it's readable for the user.

The task is set up as follows:

A1: Test [ Type:File Name Data:DSD/Lib/dsdwado.txt Store Result In:
%FNAME Continue Task After Error:On ]
A2: If [ %FNAME ! Set ]
A3: Goto [ Type:Action Number Number:17 Label: ]
A4: End If
A5: Variable Add [ Name:%LINE Value:1 Wrap Around:0 ]
A6: Read Line [ File:DSD/Lib/dsdwado.txt Line:%LINE To Var:%PNAME
Continue Task After Error:On ]
A7: If [ %PNAME ~ EOF ]
A8: Variable Clear [ Name:%PNAM*/%LINE Pattern Matching:On ]
A9: Goto [ Type:Action Number Number:17 Label: ]
A10: End If
A11: Test [ Type:Package Name Data:%PNAME Store Result In:%PNAMEL
Continue Task After Error:On ]
A12: Write File [ File:DSD/Lib/dsdwador.txt Text:%PNAMEL Append:Off ]
If [ %LINE = 1 ]
A13: If [ %PNAMEL Is Set ]
A14: Write File [ File:DSD/Lib/dsdwador.txt Text:%PNAMEL Append:On ]
If [ %LINE > 1 ]
A15: End If
A16: Goto [ Type:Action Number Number:5 Label: ]
A17: Perform Task [ Name:DSDWLAA Stop:On Priority:9 %par1: %par2:]

If the above makes sense, the %LINE increases by 1 each time until it
returns EOF, then it goes on to perform another task. If it matches
the package name it writes it to the text file dsdwador.txt, then goes
back to the top of the loop to check the next %LINE. If it doesn't
match the package name (%PNAMEL isn't set), it doesn't write the entry
and returns to the top of the loop.

In dsdwador.txt I end up with an output of:

Facebook
Tasker
Beautiful Widgets
Etc

Which is exactly what I want, howevever it takes quite a while to run
which I assume is down to the process of: read a line, write a line,
read a line, write a line and so on and so forth...

So, I think there must be a better way to do this using an array? Get
the data into an array, get Tasker to check each segment of the array,
exporting it to another array if it's matched and then writing the
array to a file in one action perhaps...?

But, I can't get my head around how do it....

If you (or anyone else), have any ideas of how to, please do hit me
with them! Thanks in advance!

>Out of curiousity, what is your project about? Is this the Android deep
> sleep / running process thing you were working on?

Yup, up to V14 now - I'm pretty sure it's the closest you can get to a
fully functional application built with Tasker... V14 is due out soon
and has automatic updates, error logging, dynamic shell scripts etc
and it most importantly detects and names rouge wakelocks between
display on/off cycles in real time. There's not an actual Android app
that does that.. yet. I'm pretty proud of it and will keep improving
it over time as a bit of a hobby...

TomL

unread,
Jan 19, 2012, 2:32:04 PM1/19/12
to tas...@googlegroups.com
Have you tried using a shell command to process dsdwado.txt file into dsdwador.txt directly, without using Tasker to iterate through each line of dsdwado.txt ?

The command:

sh /system/bin/pm 

... run as root might be a good starting point for a faster shell solution.

Tom

brandall

unread,
Jan 19, 2012, 2:47:59 PM1/19/12
to Tasker
> Have you tried using a shell command to process dsdwado.txt file into dsdwador.txt
> directly, without using Tasker to iterate through each line of dsdwado.txt ?
>
> The command:
>
> sh /system/bin/pm

Thanks Tom, but I can't see how I could convert the package name to
the application name? wado.txt is populated at display off by a script
running the following:

dumpsys power | grep PARTIAL_WAKE_LOCK | sed 's/.*\(pid=[0-9]*\))/\1/'
> $wakeinfo

while read line; do
eval "$line"
/data/DSD/./toolbox ps | awk -v pid=$pid '{if ($2==pid) print $9 }' >
$wado
done < $wakeinfo

Therefore when the display comes back on, if deep-sleep has been
interrupted I have the wado.txt containing historic data, otherwise
it's discarded. I don't know how Pent converts the package name in the
'test' action, but I couldn't think of a way to mimic the action in
shell. I don't know of a command that details the package/application
name to reference?

TomL

unread,
Jan 20, 2012, 12:35:36 PM1/20/12
to tas...@googlegroups.com
To speed up the loop, I would look really hard at any global variables being used and replace them with local vars.  You'll get a performance boost there.

The package.fullname to PackageName mapping is static, and can be done once, and only done again after a new package is installed/deleted.  You can have Tasker create once a translation file trans.sed:

s/com\.facebook\.katana/Facebook/
s/com\.android\.vending/Market/
etc...

Then you can use the shell command: sed -f trans.sed < original.txt > translated.txt 
 to do the translation each time.

Lastly, I would do some decoupling in your actions.  The code snippet you shows that runs after every "display on", that code should focus on gathering the raw dumpsys data into a file  dsdwado.txt.  Have another profile watch for the creation of that file and have it do the translation into dsdwador.txt only when the user wants to see it.  This way, the two parts (gathering, translating) can work separately and independently.

Tom

brandall

unread,
Jan 20, 2012, 8:12:14 PM1/20/12
to Tasker
> To speed up the loop, I would look really hard at any global variables
> being used and replace them with local vars.  You'll get a performance
> boost there.

Unbelievable! It almost halved the task time! I really didn't realise
they used up so much more resource... Thanks for pointing it out
again :) On a perhaps temporary downside, not being able to debug in
the variable editor is a problem with such a lot of tasks/actions.
Perhaps the debug log that pent has been working on will give me an
alternative...?

> The package.fullname to PackageName mapping is static, and can be done
> once, and only done again after a new package is installed/deleted.  You
> can have Tasker create once a translation file trans.sed:
>
> s/com\.facebook\.katana/Facebook/
> s/com\.android\.vending/Market/
> etc...
>
> Then you can use the shell command: sed -f trans.sed < original.txt >
> translated.txt
>  to do the translation each time.

I need to have a look at this in more detail, thank you. An output
from shell with both listed is what I really need...
>
> Lastly, I would do some decoupling in your actions.  The code snippet you
> shows that runs after every "display on", that code should focus on
> gathering the raw dumpsys data into a file  dsdwado.txt.  Have another
> profile watch for the creation of that file and have it do the translation
> into dsdwador.txt only when the user wants to see it.  This way, the two
> parts (gathering, translating) can work separately and independently.


The command above does actually run at display off and populates the
file then. It's discarded otherwise, so I think I'm already doing what
you're suggesting?

I did manage to get my head around the array

AAAtest (75)
A1: Test [ Type:File Name Data:DSD/Lib/dsdaa.txt Store Result In:
%fname Continue Task After Error:On ]
A2: Goto [ Type:Action Number Number:16 Label: ] If [ %fname ! Set ]
A3: Read Paragraph [ File:DSD/Lib/dsdaa.txt Para:1 To Var:%daarr
Continue Task After Error:On ]
A4: Variable Split [ Name:%daarr Splitter: Delete Base:On ]
A5: Variable Set [ Name:%arrc To:%daarr(#) + 1 Do Maths:On
Append:Off ]
A6: Test [ Type:Package Name Data:%daarr1 Store Result In:%pnd
Continue Task After Error:On ]
A7: Variable Add [ Name:%pnl Value:1 Wrap Around:0 ]
A8: Array Pop [ Variable:%daarr Position:1 To Var: ] If [ %pnl <
%arrc ]
A9: If [ %pnd Is Set ]
A10: Array Push [ Name:%pnrr Position:1 Value:%pnd Fill Spaces:Off ]
If [ %pnl < %arrc ]
A11: End If
A12: Goto [ Type:Action Number Number:6 Label: ] If [ %pnl < %arrc ]
A13: Variable Join [ Name:%pnrr Joiner:, Delete Parts:On ]
A14: Write File [ File:DSD/Lib/dsdaar.txt Text:%pnrr Append:Off
Continue Task After Error:On ]
A15: Variable Clear [ Name:%pnl/%arrc*/%daarr* Pattern Matching:On ]
A16: Flash [ Text:%pnrr Long:On ]
A17: Stop [ With Error:Off ]

It actually runs no quicker though which is a little surprising... It
must be the test action that is time consuming, rather than the read/
write line? Regardless, I'm happy as it's quick enough using local
variables! And I saved BossMan a headache too!


Reply all
Reply to author
Forward
0 new messages