Is there a automatic way to get the index of a For Loop

The only way I know is like above. Is there a default way where Thunkable tells the index count? I mean like in C++:
for (int i = 0; i < 10; i++) {
// Here I can break in the mid and the index would be value of i

Yes, the count with i from _ to _ block is exactly for this purpose.

After you add that block to your project, i will be a variable you can drag into your project from the Variables drawer. It will contain the index (count) of the loop.

1 Like

Thanks you Tatiang but I have a question. This app is just to demo my usage. This app works fine and when it breaks out I get the index value as 3 in OutputTxt which is right. My request is how do I fit in the β€œcount with i …” block into my program? If I can, then I will get rid of the Index variable and the manual way I am keeping track of the index. Please note I do need the β€œfor each item …” block and the if (j = &) block.

  1. Shouldn’t OutputTxt’s Text be 4, not 3, since β€œ&” is the fourth element in the list?

If your lists only contain one β€œ&” symbol, there’s a much easier way to do all of this that doesn’t require loops… using the β€œfirst occurence of” list block (see this: Lists - Thunkable Docs):

If your list may contain more than one β€œ&” symbol then you’ll need to use a loop. Just replace the β€œfor each item j” with a count from 1 to length of app variable InputList and then check to see if the list item at #i = β€œ&”. If so, set OutputTxt’s Text to i.

I’m using i as the loop variable but you may end up using a different letter; it doesn’t matter as long as it’s consistent within your blocks.

1 Like

Hi Tatiang,
That is brilliant solution, I tried it but could not make it work. Then Googled and Googled and came up with solution but it is wrong. The issue is it is doing 1-10 times 10 = 100. That is the reason my outputs are corrupted. The #i did not work. I spent couple of hours and finally seeking your help again. I have made it more realistic to my goal. Please advice

your for-item loop is INSIDE your count-with-i loop so for i=1, you use the for-loop and visit all 10 elements in InputList - for i=2, you do the same thing - so you would have travelled the one list 10 times.

just stick with the count-with-i loop and you will always get the correct index at anytime (it will be at the i-th position - and when you want to know what item you got atht eh i-th position, just use this block

can you describe what you are trying to do? are you using some kind of table look up such that β€œ%” always returns 234 and β€œ@” always return 86, etc? - are those symbols annunciators of some kind? like after β€œ%” you get latitude and longitude?
you can probably use objects in a clearer way to do something like that.

1 Like

Thanks a lot ManyOne. I did some work still could not figure it out.
Very sleepy will work on this in the morning. The output I want to see is this:
FoundTxt: Found % at index 1 Found & at index 4 Found @ at index 6 Found 56 at index 8
OutputTxt:45 234 467567 467567 56 89
Please advice, thanks

is your input always like this? (are the symbols always in the same order? I)
β€œ40”, β€œ%”, β€œ50”, β€œ60”, β€œ&” ,β€œ70”, β€œ@”, β€œ80”, β€œ$”, β€œ90”
can it be like this? (can there be 0 or n number before each symbol?)
β€œ40”,β€œ50”, β€œ%”, β€œ66”, β€œ&” ,β€œ77”,β€œ88”,β€œ99”, β€œ@”, β€œ111”, β€œ$”, β€œ129”,β€œ200”
or like this? (can the symbols be in any order - is it ok t have zero numbers between symbols"?
is the string you are analyzing generated by machine?

1 Like

Please see this modification to your project (the first link you shared) and let me know if that’s what you had in mind:

Why are you wanting the β€œ%” to be found at index 1? Do you realize that Thunkable lists start at index 1 and that the β€œ%” is actually in item #2 of your list? Many programming environments start with list index 0 so that may be part of the confusion here.

1 Like

Wow thanks Tatiang you made my day!! :pray:. ManyOne thanks to you also helping me with the right block to use. Now Good morning to you both, let me clarify both of your queries from earlier replies and the current ones. I am from IBM punch card era :slight_smile: Later on C++ and System programming(At Processor-OS interaction level) world. All my 40 + years of programming life starting index is 0 or you can say bit 0. So the slight confusion between us. I tend to think all programming issues in C++ way hence get caught in my own web. You people think in thunkable way a very abstracted way that is just brilliant. The data, demarkers and their positions are all fictitious created just to illustrate my problem. They are totally random. Thanks once again for both of you. I will get back to implementing it in my real program!!

1 Like

Hi Manyone,

Thanks for the good tip and it got me almost to the solution then I had to sleep, I have answered your questions in the reply to Taitiang. As I said this is just some random data to illustrate my issues.


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.