Fed up with getting caught out on looping

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
chris25
Posts: 354
Joined: Tue Oct 08, 2013 9:32 pm

Fed up with getting caught out on looping

Post by chris25 » Fri Nov 01, 2013 11:25 pm

Basically I have tried all that I know, I only added the "EXIT MOUSEUP" because I thought that would prevent the stack from going into an endless loop where the answer box would not disappear and I had to force quit livecode everytime. I know there is no mouseUp but really, how do you prevent the infamous endless loop with an answer box - I imagine that there is a straightforward code procedure for this.

on closefield
if me is not empty then
repeat until (me > 0.1 and me < 200000)
put empty into me
answer "Please enter a valid numerical value"
-----exit mouseUp
end repeat
end if
end closefield

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Fed up with getting caught out on looping

Post by dunbarx » Sat Nov 02, 2013 1:24 am

Hi.

So how does this script exit? If you have a value that is between your limits, there is no way out. Have you stepped through the code?

To help you work this out, using the core of your current script, add this:

Code: Select all

on closefield
   if me is not empty then
      repeat until (me > 0.1 and me < 200000)
         put empty into me
         answer "Please enter a valid numerical value"
         if the optionkey is down then exit to top
      end repeat
   end if
end closefield
You can now hold down the optionKey to exit. Note that any reference to a "mouseUp" handler is bogus. How would LC find something meaningful with that?

By the way, this is great learning stuff. Your being trapped in a loop is as cruel a way to work through this, but it will make you stronger. And by the way again, what are you trying to achieve?

Craig Newman

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am

Re: Fed up with getting caught out on looping

Post by snm » Sat Nov 02, 2013 7:14 am

What about 2 buttons OK and Cancel:
OK - check if valid, do nothing if not
Cancel - close dialog without entry

Marek

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Fed up with getting caught out on looping

Post by Simon » Sat Nov 02, 2013 8:10 am

Hi Chris,
You said it... Looping.
With your "until" you made it loop until the the equation is fulfilled. There is also a "repeat forever", pretty much what you set up because once into the repeat loop "me" is never updated. The repeat is never exited.

Which leads to the other thing you wanted to know:
exit repeat

In the end, using a repeat is not what you want there, you can figure out a better way. If not just ask.

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

chris25
Posts: 354
Joined: Tue Oct 08, 2013 9:32 pm

Re: Fed up with getting caught out on looping

Post by chris25 » Sat Nov 02, 2013 9:39 am

Hallo, Craig, SNM and Hi Simon. I typed in first Craig's code to learn, I was confused, I did understand what it was I was to supposed to be seeing - except for the same thing. I am sure that you, Craig, know what you are trying to tell me but really - I am only 3 weeks into programming having never seen a code in my life except for the one I type into my bank account! So please tell me how could I have figured something out by quitting with the optionKey?

SNM - thanks but at this moment I am trying to learn, I could have done what you said, but it is untidy in this particular flow of routine.

Simon - I cheated - :cry: I went to the code of that Print box stack and looked at what you gave me once.

Why was I stuck? apart from ignorance, simply because the bloody dictionary for the word 'Repeat' needs another dictionary to explain the darn explanations for people like me! Really, Let's use non-programming language to explain issues not the very language that confuses in the first place. Yes yes I know this is fine for programmers - or maybe I saw the word 'dictionary' when it is really a programmers reference tool, a bit like the programmers scrapbook - Ok Ok I'm a moaner, and my cat is still sleeping - my next stack? A moaning manual for beginners - with a new command: onMoanup exit moanUp, with no options for looping.

On closefield
if me < 99 or me > 5001 then
put empty into me
answer "Please enter a value between 100 and 5000"
end if
end closeField

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Fed up with getting caught out on looping

Post by dunbarx » Sat Nov 02, 2013 5:08 pm

Hi again.

You are undertaking a great task. If you have no programming experience at all, then know, and console yourself, with that. This is not just learning how to use your fancy new TV remote. It is rather learning a new field of technology.

And you are slightly correct that the learning materials are unable to lead a new user through the process in the same way a personal tutor could. But they are not that bad, as you will one day soon admit, when you have more experience, and the poor dictionary does not seem so opaque.

In my script, I asked if you had stepped through it. I should have asked if you knew how to do that. Do you? Ask right away if not. But if you do, you would see that the code runs as advertised, but with no way to exit the loop. Adding the line about holding down the option key gave a manual way to do that.

And as the others have said, and this is a good thing, there are many ways to accomplish what you want. I would like you to use your loop thing, because it is a good learning lesson. When you have that down, you can try simpler ones.

So in your loop, think about changing your "answer" command, to an "ask" command, that takes the input from the user and does one thing with it if it is within range, and another if not. Look up the "if/then" control structure in the (shudder) dictionary. It will be your best friend one day soon, and give you the tools to manage that fork you will create. And make sure that either fork does not send the code back to its starting point, ad infinitum.

This community has endless patience for those who persevere. Do be one of those. Try us. Write back...

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Fed up with getting caught out on looping

Post by jacque » Sat Nov 02, 2013 7:15 pm

chris25 wrote:Why was I stuck? apart from ignorance, simply because the bloody dictionary for the word 'Repeat' needs another dictionary to explain the darn explanations for people like me! Really, Let's use non-programming language to explain issues not the very language that confuses in the first place.
It's true that the dictionary assumes a basic familiarity with core programming concepts. These are things that are common to all programming languages. Let's walk through your first attempt:

Code: Select all

on closefield
  if me is not empty then
    repeat until (me > 0.1 and me < 200000)
     put empty into me
     answer "Please enter a valid numerical value"
     -----exit mouseUp
    end repeat
  end if
end closefield
Catching the closefield message was exactly the right thing, so that's good. However, you can only use "exit" to leave the currently running handler, and the current handler is not a mouseUp one, so that's why you get an error there. You'd want "exit closefield" if you used that approach. That would work here, but we won't use it because exiting immediately after the first repeat is the same as not having a repeat at all, so there's no reason to use repeat.

A repeat loop goes around and around until the condition at the top is matched or until you explicitly "exit repeat" in your script. You could have used "exit repeat" in your handler instead of "exit mouseUp" but that's as inefficient as "exit closefield" -- it exits the loop during its first iteration, so there's no reason to loop in the first place.

Your loop declares a condition that is valid if a number falls between a defined range. If a user enters an incorrect number, the repeat loop begins. It puts empty into the field (good.) It tells them what the error is (good.) That's the end of the repeat block, so it cycles back to the top again. It looks at the content of the field to see if it matches the condition. The field is empty now, so it doesn't meet the requirements. The repeat loop empties the field again (redundantly, but no harm) and tells the user they're wrong. The cycle continues forever because there is never an entry in the field; the repeat loop continually re-empties it.

I think you've seen how to avoid that now.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

chris25
Posts: 354
Joined: Tue Oct 08, 2013 9:32 pm

Re: Fed up with getting caught out on looping

Post by chris25 » Sat Nov 02, 2013 8:11 pm

Craig, and Jacque. Jacque your explanation really laid it down in black and white, just like seeing it on a blackboard. Of course the concept of loop and repeat is not difficult at all to understand, but in the world of programming your explanation really made it clear and that clarity was a lot different from the mental concept that underlined my inability to grasp what was going on in code. thankyou Jacque.

Craig, I will definitely try out your suggestions with the commands you gave, I will make a copy of my now finished stack and mess around to see if I can not code it somewhat differently slightly.

I do have though one niggly situation that has plagued me for some time now on one text field where the user is invited to submit numbers. It is the only text field in the whole stack that does this, and all the text fields have the same properties - When you first open the stack you can put your cursor in that field and type, but when you return to that text field from another card you can not. The only way I can place the cursor into that field is to click on selection tool, click inside text field to select, click back on browser tool and then click inside text box. Or I have to click uoutside of the livecode environment and then click back into the text box. Now everything works, the stack performs 100%, except for this one tiny thing. I enclose screenshots of the text field called "pixels" and the "continue" button just in case you see something. Oh I also checked the "tab on return" in the properties box just to see if that worked by me pressing the tab key, and yes the cursor immediately appeared in that box. But I can see no reason why this would be happening. Any thoughts?

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Fed up with getting caught out on looping

Post by jacque » Sun Nov 03, 2013 6:14 am

No clue. It acts like something is covering the field, a transparent object maybe. Though why it should remove itself when the stack suspends doesn't make sense.

Show us a screen shot of the field's property inspector open to the Basic pane, maybe we'll see something.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

chris25
Posts: 354
Joined: Tue Oct 08, 2013 9:32 pm

Re: Fed up with getting caught out on looping

Post by chris25 » Sun Nov 03, 2013 10:37 am

Hallo Jackque, here is the properties box for that text field

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Fed up with getting caught out on looping

Post by jacque » Sun Nov 03, 2013 6:45 pm

Okay, that helps. Turn on autohilite. That's what allows you to select text with the mouse. An insertion point is technically a selection.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

chris25
Posts: 354
Joined: Tue Oct 08, 2013 9:32 pm

Re: Fed up with getting caught out on looping

Post by chris25 » Sun Nov 03, 2013 7:45 pm

Thankyou Jacque for this.

Post Reply