Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Can we have a variable as increment in a for each loop

79 views
Skip to first unread message

Bernard Haddad

unread,
Dec 11, 2018, 11:25:06 AM12/11/18
to MIT App Inventor Forum
Hello,
I am building a loop to do a certain action (calling an internet site) for each element in a list.
Since I don't know how long it takes to get the result I fire a timer and I process the action when the result is ready.
To do the action for the second item in the list I have to wait until it is ready.
I built various versions of for each loops:
- in one version I put a variable as the increment, starting this variable with 0. the variable is then put to 1 when the other (Asynchronous action) is done. and then inside the loop I put the variable back to 0.
it looks like we NEVER go inside the loop.
- in another version I intialized the increment variable to 1, and the variable is then put to 0 inside the loop. The loop does not "pause" and runs up to the end, as if the value of the increment is never changed.

So the question is, can we use a variable as increment in a for each loop?

second question, how can I run such a loop by "pausing" between the iterations, wthout going to building the loop myself step by step..
thanks,
Bernard

Taifun

unread,
Dec 11, 2018, 11:43:48 AM12/11/18
to MIT App Inventor Forum
use a clock component rather than a loop


Taifun

Trying to push the limits of App Inventor! Snippets, Tutorials and Extensions from Pura Vida Apps by Taifun.        

Chris Ward

unread,
Dec 11, 2018, 12:06:48 PM12/11/18
to MIT App Inventor Forum
Hi Bernard

Perhaps you can show us your Blocks. From your description in Question 1, you talk about changing the increment of Loop, mid-loop. You can't do that safely in any programming language. You can however test for a Boolean Change and exit the Loop based on the result.

Second question - I don't understand it!

Abraham Getzler

unread,
Dec 11, 2018, 1:59:04 PM12/11/18
to MIT App Inventor Forum
Copy the list to a work list, and
process the front (item 1) of the list.
Remove item 1 from the work list after done with it.

When you are ready, process the new item 1 of the work list.

If the work list is empty, you are finished.

ABG


Bernard Haddad

unread,
Dec 11, 2018, 2:46:04 PM12/11/18
to MIT App Inventor Forum
Thank you all.
actually I used a clock component already but just for one specific action and that worked well.
Then for this case I had to repeat the process several times and I had to wait for the first action before going to the second one.
I finally put a part of the process, notably the incrementation computation in the clock component and it worked.

In my first idea I wanted to use a for each loop or a For each item in the list but when the loop starts there is no way to tell the loop to stop or wait.
And Exiting (what instruction should I use for that?) might not help since I have no way to come back to the loop with the right index value.
Or maybe I am wrong somewhere.

Abraham Getzler

unread,
Dec 11, 2018, 3:06:51 PM12/11/18
to MIT App Inventor Forum

Chris Ward

unread,
Dec 11, 2018, 5:23:17 PM12/11/18
to MIT App Inventor Forum
.... and to complete the info: To exit a Loop, use the "break" Block (found at the bottom of the Control Blocks Palette).

TimAI2

unread,
Dec 11, 2018, 5:48:20 PM12/11/18
to MIT App Inventor Forum
If you are using the web component, and from what you say getting a result from your list items action, can you use the response or responseContent received (webGotText) to initiate the next list item action ?

This is a bit similar to Taifun's prefetch filebyfile scenario, which relies on only using one web component for the job (the other web components are for setting things up).

Worth a look...

Bernard Haddad

unread,
Dec 12, 2018, 4:33:03 PM12/12/18
to MIT App Inventor Forum
Yes, I finally did something similar.
But in the first shot I was reluctant to build something where Proc1 calls Proc2 which calls Proc1 etc.
Is there a limit in the number of "steps" that can be chained this way?

ABG

unread,
Dec 12, 2018, 6:50:50 PM12/12/18
to MIT App Inventor Forum
But in the first shot I was reluctant to build something where Proc1 calls Proc2 which calls Proc1 etc.
Is there a limit in the number of "steps" that can be chained this way?

I've recursed 25 levels deep in

But it's not officially supported.

ABG

Reply all
Reply to author
Forward
0 new messages