For each... from to by - Variable is not bound in the current context

212 views
Skip to first unread message

Publius

unread,
Oct 23, 2017, 6:19:26 AM10/23/17
to MIT App Inventor Forum
I am receiving an error using the 'Do it' function and of course the app is not working properly.

What I am trying to achieve is subtract two numbers, both are varible numbers.

One number comes from a procedure called groupA which functions properly.
The second variable comes from a 'For loop' as follows.


First of all, why is that local variable not accepted in its own 'do' function? How can I fix this?
Is there a way of monitoring each From/To/By iteration?

I have tried with static numbers and works perfectly, it has a problem with the 'For each' iteration.
I also have tried to read those 8 to 0 by -1 values from a list and it was giving bad arguments.

So any thoughs are welcome.

Ghica

unread,
Oct 23, 2017, 8:53:01 AM10/23/17
to MIT App Inventor Forum
What does procedure groupA look like?
Cheers, Ghica

SteveJG

unread,
Oct 23, 2017, 9:18:27 AM10/23/17
to MIT App Inventor Forum
DoIt does not work on local variables.  DoIt only works with global variables and then only when you are connected to a device or an emulator. 

 To capture a local value, create a dummy global variable. Set the dummy to the value of the local variable you are interested in monitoring within the event handler code  Use DoIt with the global dummy variable to get the value of the local variable..

Be aware, AI processes data asynchronously.  Your  loop has not yet completed when your code attempts to process the equation.  You are going to have to calculate your powers a different way.

Publius

unread,
Oct 23, 2017, 1:05:16 PM10/23/17
to MIT App Inventor Forum
To Ghica.
Procedure groupA is a code parsing procedure which inputs part of a scanned qr code, the code uses an integer from text method, in order to convert text into a number. The groupA procedure works perfectly is already cross-checked and always checks as supposed. Therefore I don't think that the error lies in that procedure.

To SteveJG
I have turned the 'Do it' function over into a global variable but still, the app does not work as supposed. In fact what I am experiencing all the time is that only the final state of the for loop works. Namely, there should be a totally different result at iteration 2^8 which is the initial state and a totally different result for iteration 2^0 which is the final state. I can only obtain what's happening in the final state alone. Keep in mind that with static numbers the app works as supposed. Namely, if I do not use a for loop but insert the numbers of that for loop manually, everything works great.

I cannot understand the asynchronously you mention. AI2 is a single threaded abstract layer of the Android language. As I understand everything is processed at a specific time, once their procedure/operation/result is called upon execution. Please, can you elaborate on that?

What do you mean "the loop has not yet completed when your code attempts to process the equation"? I think that is exactly what that for loop should do for example in C what I would do would look like...

int main {
int powers, powersofTwo, groupAloop, groupA;

for (powers=8; powers>=0; powers--) {

powersofTwo = 2^powers ; 

groupAloop = groupA - powersofTwo

  }
}

Namely, each iteration from 8 to 0 is stored in local variable powers. So in first time loop, variables are set as follows
  1. powers=8 --> powersofTwo=256 --> groupAloop=groupA - 256
  2. powers=7 --> powersofTwo=128 --> groupAloop=groupA - 128
  3. powers=6 --> powersofTwo=64 --> groupAloop=groupA - 64
  4. powers=5 --> powersofTwo=32 --> groupAloop=groupA - 32
  5. powers=4 --> powersofTwo=16 --> groupAloop=groupA - 16
  6. powers=3 --> powersofTwo=8 --> groupAloop=groupA - 8
  7. powers=2 --> powersofTwo=4 --> groupAloop=groupA - 4
  8. powers=1 --> powersofTwo=2 --> groupAloop=groupA - 2
  9. powers=0 --> powersofTwo=1 --> groupAloop=groupA - 1
Sorry for the detail, I am sure you had it from "hello", I just wanted to be as clear as possible of what I want to achieve. Probably I should make a procedure that would be very long in blocks instead of dividing it for better monitoring, I will try it a let you know.

If you have any insights, I am all ears.
 

 

Ghica

unread,
Oct 23, 2017, 2:38:22 PM10/23/17
to MIT App Inventor Forum
I think Steve is right, you have a synchronization problem.
Maybe it helps to set a new variable to the result of groupA and then use the value of that variable in your calculation. That also helps to know whether the result of groupA is what you think it is by using DoIt.
Cheers, Ghica

Ghica

unread,
Oct 23, 2017, 3:00:24 PM10/23/17
to MIT App Inventor Forum
>>AI2 is a single threaded abstract layer of the Android language.
This is not true in two ways. There is not something linke the Android language, if anything that would be Java.
App Inventor does not have Java at its core but is based on Blockly.
And, more importantly, App Inventor is event based. Everything depends on events raised, like button clicks, files read, list view opened etc. therefore it is quite possible that things happen in parallel in ways that are not always easy to see.
Cheers, Ghica

SteveJG

unread,
Oct 23, 2017, 3:04:02 PM10/23/17
to MIT App Inventor Forum
You might use a List instead of a powersofTwo variable to  get what you want.   Add a value to the list each time the list progresses. (Add items to list   powersofTwoList
                                                                                                                                                                                                                                                 power  
 
then work with the powersofofTwoList to use the value you want.


As your blocks are written the loop calculates using a value of 8, then 7 then...etc to the last one.  When you calculate at 8 the procedure calculates a powersofTwo, however, when you calculate at 
7, it also calculates but REPLACES the value stored in powersofTwo  until finally it calculates the final value and substitutes that answer...so you get the value of the final state because that is what 
you programmed the routine to do.

-- Steve

Publius

unread,
Oct 24, 2017, 4:34:10 AM10/24/17
to MIT App Inventor Forum
While writing the previous post with the example in C, it occurred to me that what I wanted was not what I programmed AI2 to do. The problem was that I was calling a function OUTSIDE the for loop, which was probably running correctly. The code blocks were placed in such a way, where static numbers/powers always checked out but proved to be in error once run in a variable form as the 'for loop' is. Great misconceptions!!!

The solution is as follows... though it introduced another problem which has nothing to do with the current post. I'll scrutinise it a bit and come back if all else fails.



Thank you all for your contribution.
Best regards
Reply all
Reply to author
Forward
0 new messages