To repeat or not to?

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
HJay
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 85
Joined: Sun Mar 04, 2012 12:16 pm

To repeat or not to?

Post by HJay » Tue Apr 03, 2012 8:53 pm

Hi

Could someone explain the repeat command to me? I want to add up a score each time a correct answer is given. I can determine if the answer is correct and place a 1 in a field. I can then add the next correct answer to this but only by actually putting the same lines of code in again. I have played around with the repeat command but so far have just managed to crash LiveCode by getting in some eternal loop or something :D

Many thanks

HJay

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: To repeat or not to?

Post by mwieder » Tue Apr 03, 2012 9:42 pm

but so far have just managed to crash LiveCode by getting in some eternal loop or something
Hah! Yes, unfortunately that's easy to do in *any* programming language. Try sticking this in your repeat loop:

Code: Select all

if the mouse is down then
  exit repeat
end if
Can you post your repeat loop (or at least part of it)? Explaining the repeat loop can take many forms, and it will probably take shape better if we can start with your real-world example.

Gene
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 75
Joined: Wed Mar 09, 2011 6:48 pm

Re: To repeat or not to?

Post by Gene » Tue Apr 03, 2012 9:59 pm

I can't resist passing along this quote from a much longer piece about how different disciplines (mathematicians, physicists, computer programers, etc.) hunt elephants in Africa. At does make a fun statement about repeat loops, so I guess it's valuable to remember:

"COMPUTER SCIENTISTS hunt elephants by exercising Algorithm A:
1. Go to Africa.
2. Start at the Cape of Good Hope.
3. Work northward in an orderly manner, traversing the continent
alternately east and west.
4. During each traverse pass,
a. Catch each animal seen.
b. Compare each animal caught to a known elephant.
c. Stop when a match is detected.

EXPERIENCED COMPUTER PROGRAMMERS modify Algorithm A by placing a known elephant in Cairo to ensure that the algorithm will terminate."


There's your repeat loop. Just don't ever forget to put the elephant in Cairo, or the amount of time it takes for your computer to seize up if it doesn't find an elephant somewhere is just about directly proportional to the processor speed.

Seriously, I hope someone posts a simple example for you. For me, that is always the best way learn a new facet of programming. If no one jumps in, I'll post my best effort, although I hope that I don't do you more harm than good.

Gene

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: To repeat or not to?

Post by mwieder » Tue Apr 03, 2012 10:18 pm

Gene-

The only reason I refrained from posting an example yet is that I think it will make more sense in the context of the real script. There are so many variations of the repeat command that I figured posting something that didn't directly apply would muddy the waters even more.
One morning I shot an elephant in my pajamas. How he got into my pajamas I'll never know.
-- Groucho Marx

Gene
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 75
Joined: Wed Mar 09, 2011 6:48 pm

Re: To repeat or not to?

Post by Gene » Wed Apr 04, 2012 12:23 am

mwieder wrote:Gene-

The only reason I refrained from posting an example yet is that I think it will make more sense in the context of the real script. There are so many variations of the repeat command that I figured posting something that didn't directly apply would muddy the waters even more.
One morning I shot an elephant in my pajamas. How he got into my pajamas I'll never know.
-- Groucho Marx
Mr. meweider, anyone who can drag up a quote from Groucho Marx on a moment's notice can't be all bad! I think you are right on by not wanting to confuse the issue by offering up one example that really involves many. I say Mr. mweider because it's apparent that you and I and Groucho are all elephant shooters, albeit separated by time and distance. However, as long as we're enjoying some quotes, something else came to mind:
God, Thou great symetry,
For all my shapeless days,
Spent in shapeless ways,
Give me one perfect thing.
What I was thinking was that a single, simple example can be the ignition point for the learning curve to follow. Just one perfect thing that proves, "Wow, it works." After that, you are free to drive yourself nuts figuring out the rest.

-Gene

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: To repeat or not to?

Post by mwieder » Wed Apr 04, 2012 12:47 am

Yes, but what's holding me back from "one perfect example" is that I'm not yet convinced that the OP really needs/wants a repeat loop in the first place to generate the new score. I'm waiting for some clarity before starting off in the wrong direction.

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

Re: To repeat or not to?

Post by dunbarx » Wed Apr 04, 2012 1:04 am

HI.

I agree with Mark. It sounds like you are invoking a series of actions, each of which works independently, even though a test is made and values are accumulated along the way. Post more of what you are up to.

In general, though, repeat loops are of fundamental importance and contain immense power. Once you have your project underway, we might just see if it can be sensibly rewritten using such a construct. They are easy to understand and use. They are easy to screw up.

Craig Newman

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: To repeat or not to?

Post by Dixie » Wed Apr 04, 2012 2:13 am

Hjay...

Mark and Craig are right, it is not a repeat that you are after to add the 'correct' and the 'wrong' selections... I have modified your stack again to keep track of the scores. I have used two global variables 'gCorrect' and 'gWrong'... The way I have set this up is not the best way to go about it, but it works and does not mess with your stack too much, and I hope that it gives you some ideas. Instead of using global variables I would suggest that you use custom properties... but that is maybe for something a little later to try... but it would do no harm for you to read about variables and custom properties in the user manual...

hope it helps..

Dixie
Attachments
Work Ethic.livecode.zip
(1.92 KiB) Downloaded 262 times

HJay
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 85
Joined: Sun Mar 04, 2012 12:16 pm

Re: To repeat or not to?

Post by HJay » Wed Apr 04, 2012 9:15 am

Wow there are elephants everywhere :D and from now I will remember to always put one in Cairo :D

Thanks again Dixie that is great
I have used two global variables 'gCorrect' and 'gWrong'... The way I have set this up is not the best way to go about it, but it works and does not mess with your stack too much, and I hope that it gives you some ideas.
Great, simpler than what I was trying to do :) So a Global Variable is like a more permanent type of variable that works on all the button, fields etc. on the stack?
Instead of using global variables I would suggest that you use custom properties... but that is maybe for something a little later to try... but it would do no harm for you to read about variables and custom properties in the user manual...
Would the custom properties be used in a different way to the custom property that is already being used?

I understand that you can't 'write to' (is that the right term) a custom property you have to use a variable and then set the variable to a custom property? Did I get that right?

I am still not sure how that would keep a score?

Code: Select all

global gCorrect,gWrong

on openCard
   lock screen
   
   put 0 into gCorrect
   put 0 into gWrong
   
   put empty into fld "score"
   setTheTest
end openCard

on closeCard
   repeat with count = 1 to 3
      set the label of button count to space
   end repeat
end closeCard

on setTheTest
   lock screen
   put gCorrect & "/" & gWrong into fld "score"
   
   set the label of button "buttonL" to space
   set the label of button "buttonR" to space
   
   set itemDel to tab
   put the cNumberPairs of this card into thePairs
   sort lines of thePairs by random(the number of lines of thePairs)
   
   put random(2) into whichNumber
   set the label of button "testNumber" to item whichNumber of line 1 of thePairs
   
   if whichNumber = 1 then 
      set the label of button "ButtonR" to item 1 of line 1 of thePairs
      set the label of button "ButtonL" to item 2 of line 1 of thePairs
   else
      set the label of button "ButtonL" to item 2 of line 1 of thePairs
      set the label of button "ButtonR" to item 1 of line 1 of thePairs
   end if
   
end setTheTest
Many thanks HJay

p.s. still don't understand why the button labels are set to space? :?

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: To repeat or not to?

Post by Dixie » Wed Apr 04, 2012 11:57 am

Hjay...

Try not setting the label of the buttons to 'space' and see what happens... :D

be well

Dixie

HJay
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 85
Joined: Sun Mar 04, 2012 12:16 pm

Re: To repeat or not to?

Post by HJay » Wed Apr 04, 2012 1:46 pm

Hi Dixie

I have deleted the three lines of code that refer to setting the lable of buttons to space. I can't see anything different happening? Have opened and closed the stack a few times, still can't see anything different?

What should I see?


HJay :?
Attachments
Clicking numbers Dixie Score no spaces .zip
(1.5 KiB) Downloaded 239 times

HJay
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 85
Joined: Sun Mar 04, 2012 12:16 pm

Re: To repeat or not to?

Post by HJay » Fri Apr 13, 2012 12:39 pm

Hi Dixie

Please tell me what the reason is for setting the labels to space, it's driving me nuts :cry: I can't see any difference?

HJay

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: To repeat or not to?

Post by Dixie » Fri Apr 13, 2012 3:40 pm

Hjay...

I guess that I set the label of the fld to space whilst I built the setTest handler... If the label of the button is empty then the name is shown and it must have been annoying me. I have adjusted the stack and you will see that the name of the fld appears momentarily in the button... for me it is annoying.. just putting a space in there cured the problem...

be well

Dixie
Attachments
annoying flicker.livecode.zip
(1.88 KiB) Downloaded 234 times

HJay
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 85
Joined: Sun Mar 04, 2012 12:16 pm

Re: To repeat or not to?

Post by HJay » Fri Apr 13, 2012 5:51 pm

Thanks for putting me out of my misery there.

I don't get the name of the fld appearing in the button at all though. Maybe it’s a Mac – PC thing. My partner now thinks I'm crazy for staring at my screen so close up :D

One last thing


What does this do?

Code: Select all

on closeCard
   repeat with count = 1 to 3

   end repeat
end closeCard
Many thanks for the clarification

HJay

Post Reply