Putting loop in different procedures of code

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
mbell03
Posts: 5
Joined: Fri Feb 15, 2019 8:06 pm

Putting loop in different procedures of code

Post by mbell03 » Fri Feb 15, 2019 11:15 pm

Hi i have a question about creating a loop that starts and ends in different procedures of code
I am following Pseudocode that says i
1. Start loop
2.
3.
4.
5. End loop

Each step of pseudocode must be a different procedure
ie on initialise
end initialise

Here is the code and the error message displayed
Can you please let me know what i have done wrong or how to do this
Attachments
Capture2.PNG
Capture.PNG

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: Putting loop in different procedures of code

Post by SparkOut » Fri Feb 15, 2019 11:40 pm

I really don't understand what it is that you are trying to do. I don't see anything in your pseudocode that suggests you need a loop that starts in one handler and ends in another. This is something you cannot do given the structures you suggest in your pseudocode.

You can achieve the same effect with different commands, like "send <handlername>" but I am not at all sure that you need such a structure. I can't see what you are trying to achieve with this.

For a basic loop, the syntax is

Code: Select all

repeat with tLoop = 1 to 5 
where tLoop is a variable name. "loop" is not a part of the "repeat" command syntax - it is a given that you are looping, and the "with" form requires you to specify a variable which is incremented each iteration of the loop. (As opposed to the "repeat for each" form of the repeat loop. )

Could you explain a bit more what it is that you are trying to do with this?

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Putting loop in different procedures of code

Post by bogs » Fri Feb 15, 2019 11:50 pm

I'm with SparkOut on this, for starters, you can't start a repeat loop in one handler and end it elsewhere.

What you *could* do, though, is start a repeat loop and have it call all your handlers, but that would be pointless as it would be a 1 time loop, so it would be more efficient to just call the handlers one after the other.

The only other thing I'd add is this sounds an awful lot like a homework assignment :wink:
Last edited by bogs on Sat Feb 16, 2019 1:06 pm, edited 1 time in total.
Image

Klaus
Posts: 14196
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Putting loop in different procedures of code

Post by Klaus » Fri Feb 15, 2019 11:52 pm

Hi mbell03,

welcome to the forum!

I HIGHLY recommend to check one or two or all of these stacks to get the very basics of Livecode:
http://www.hyperactivesw.com/revscriptc ... ences.html


Best

Klaus

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

Re: Putting loop in different procedures of code

Post by dunbarx » Sat Feb 16, 2019 4:44 am

Hi.

Or better, give an actual example of code, even if that code doesn't actually do anything, but is only indicative of your intent, something like:

Code: Select all

on mouseUp
  doThis
  doThat
  repeat with y = 1 to 5
    put y into temp
  end repeat
  doSomethingElse
end mouseUp
Then we can comment on how you might build a handler that actually does some work.

Craig Newman

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Putting loop in different procedures of code

Post by bogs » Sat Feb 16, 2019 1:09 pm

You know, as I was thinking about this, it occurred to me they might not be talking about an actual loop, but merely passing a result along or having one handler call another. That is certainly possible to do and would make sense.
Image

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

Re: Putting loop in different procedures of code

Post by dunbarx » Sat Feb 16, 2019 6:15 pm

@Bogs.

The example does say "repeat", so I think that is a given. I assume the question refers to the ability in general to call command and function handlers from within other handlers, and to do so within a repeat is just a particular example.

@ mbell03:

In a button script, does this help?

Code: Select all

on mouseUp
   repeat with y = 1 to 3
      doubleNumber y
      tripleNumber y
   end repeat
end mouseUp

on doubleNumber var
   answer "Doubling" && var && "gives" && var * 2
end doubleNumber

on tripleNumber var
   answer "Tripling" && var &&  "gives" && var * 3
end tripleNumber
Pretty silly, eh? But I wanted you to see how values are passed and acted on within separate handlers.

Craig Newman

mbell03
Posts: 5
Joined: Fri Feb 15, 2019 8:06 pm

Re: Putting loop in different procedures of code

Post by mbell03 » Mon Feb 18, 2019 9:09 pm

SparkOut wrote:
Fri Feb 15, 2019 11:40 pm
I really don't understand what it is that you are trying to do. I don't see anything in your pseudocode that suggests you need a loop that starts in one handler and ends in another. This is something you cannot do given the structures you suggest in your pseudocode.

You can achieve the same effect with different commands, like "send <handlername>" but I am not at all sure that you need such a structure. I can't see what you are trying to achieve with this.

For a basic loop, the syntax is

Code: Select all

repeat with tLoop = 1 to 5 
where tLoop is a variable name. "loop" is not a part of the "repeat" command syntax - it is a given that you are looping, and the "with" form requires you to specify a variable which is incremented each iteration of the loop. (As opposed to the "repeat for each" form of the repeat loop. )

Could you explain a bit more what it is that you are trying to do with this?
Hi the reason i am specific about the pseudocode steps being the same as a handler as this is a school project and we have been told that is what it means. If this isnt possible thats fine but i wasnt sure if there was a way to do it that i didnt know about.
Basically the program spec wants to find generate a username and at the begining you are asked how many usernames you need that is why i need the loop so the program will repeat all the steps of generating the username for the amount of usernames needed.

Thanks for your assistance

mbell03
Posts: 5
Joined: Fri Feb 15, 2019 8:06 pm

Re: Putting loop in different procedures of code

Post by mbell03 » Mon Feb 18, 2019 9:17 pm

dunbarx wrote:
Sat Feb 16, 2019 6:15 pm
@Bogs.

The example does say "repeat", so I think that is a given. I assume the question refers to the ability in general to call command and function handlers from within other handlers, and to do so within a repeat is just a particular example.

@ mbell03:

In a button script, does this help?

Code: Select all

on mouseUp
   repeat with y = 1 to 3
      doubleNumber y
      tripleNumber y
   end repeat
end mouseUp

on doubleNumber var
   answer "Doubling" && var && "gives" && var * 2
end doubleNumber

on tripleNumber var
   answer "Tripling" && var &&  "gives" && var * 3
end tripleNumber
Pretty silly, eh? But I wanted you to see how values are passed and acted on within separate handlers.

Craig Newman
Unfortunately not the loop i need to create needs to be looped an amount of times entered by the user so i am unable to do that there as i get error messages

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Putting loop in different procedures of code

Post by bogs » Mon Feb 18, 2019 9:36 pm

dunbarx wrote:
Sat Feb 16, 2019 6:15 pm
@Bogs.
The example does say "repeat", so I think that is a given. I assume the question refers to the ability in general to call command and function handlers from within other handlers, and to do so within a repeat is just a particular example.
Yah, that would make sense, especially with the last 2 posts. I just know you can't start a loop in one handler and end it somewhere else.
mbell03 wrote:
Mon Feb 18, 2019 9:17 pm
Unfortunately not the loop i need to create needs to be looped an amount of times entered by the user so i am unable to do that there as i get error messages
The loop Craig showed you was to give you an example of how to do a loop and have it call other handlers. You would have to modify it to suit your additional information, but to break it down it would be modified something like this -

Code: Select all

/* you get your input from the user, probably in an ask dialog or field. 
Let us assume your user enters 5 into a field, and you have a button 
below that to start the routine. */

on mouseUp
   put field "userInput" into tNum
   repeat with y = 1 to tNum
      handlerOne
      handlerTwo
      handlerThree
      handlerFour
      handlerFive
   end repeat
end mouseUp

on handlerOne
   //your code here...
end handlerOne

on handlerTwo
   //your code here...
end handlerTwo

// etc etc...
I assume you have the code to generate whatever you are trying to generate in the handlers already.
Image

mbell03
Posts: 5
Joined: Fri Feb 15, 2019 8:06 pm

Re: Putting loop in different procedures of code

Post by mbell03 » Mon Feb 18, 2019 10:21 pm

Ok so from this example is the code for the user input before the onmouseup ?

Klaus
Posts: 14196
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Putting loop in different procedures of code

Post by Klaus » Mon Feb 18, 2019 10:24 pm

No, everything between /* and */ is a comment.

Do yourself and us a favour and read up this link:
viewtopic.php?f=7&t=32177#p176592
Currently we are apparently not talking the same language, which makes it hard to communicate correctly.

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Putting loop in different procedures of code

Post by bogs » Mon Feb 18, 2019 11:57 pm

Klaus has a point there, and he already explained that the top text is a comment :wink:

There is no code needed for the field "userInput", everything there happens in the button script.

See if this hits closer to what your trying for.
userNames.zip
Hmm.
(1018 Bytes) Downloaded 195 times
Image

mbell03
Posts: 5
Joined: Fri Feb 15, 2019 8:06 pm

Re: Putting loop in different procedures of code

Post by mbell03 » Thu Feb 21, 2019 8:38 am

Thanks for everyones help i have figured that out annoyingly there must be errors with other parts of my code but an error that doesnt make sense to me is a bad command error for using indentation ??

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Putting loop in different procedures of code

Post by bogs » Thu Feb 21, 2019 2:15 pm

If you can post the actual error and the code you are using for the indentation, we might be able to tell you what the problem is :)
Image

Post Reply