Jump to content

Easy Loop Question


KLA
 Share

This topic is 7284 days old. Please don't post here. Open a new topic instead.

Recommended Posts

I've never used looping in a script before. That's for repeating script steps, right? When i add "Loop" to a script, does it repeat the steps above it or below it? I only need it to loop part of the steps i have, (omitting the first two steps of my script), so, I'm hoping it's below so i can just add it before the steps i need looped.

Link to comment
Share on other sites

A loop is a common construction in any programming language. You need 3 pieces:

Loop (the start of the loop)

Exit Loop If (how you break out of the loop)

End Loop (the end of the loop)

Everything between Loop and End Loop gets executed until and repeatedly you exit the loop. The Exit Loop statement can be placed anywhere within the loop based on what you're trying to accomplish.

Link to comment
Share on other sites

When using loops, the steps that are repeated are those that are under 'Loop' but above 'End Loop' Scriptmaker will even indent all script steps that are inside the loop (meaning under 'Loop' but above 'End Loop') which makes it easy to read and to find out which steps are being "looped" or repeated. When dealing with loops, always remember to include the 'Exit Loop If' command so that you can trigger when your loop will end.

Small example...

Set Field ( gCounter = 0 )

Loop

Set Field ( gCounter = gCounter + 1 )

Exit Loop If ( gCounter = 3 )

End Loop

Show Message ( "Done" )

The script starts by setting a global (number) field to 0. It then enters the loop where gCounter is incremented by 1 (1 is added to itself.) The loop will continue incremented gCounter until it equals 3, which will then exit the loop and move to the next script step. In the example, once gCounter is equal to 3, a message is shown with the text "Done"

Hope this helps.

Link to comment
Share on other sites

ok. figured that out. Now my problem is that my script is getting stuck in a loop. My main script (in the master file) is basically performing an external script in the related file that goes through each record( in a found set) in that related file and one by one, goes back to the master file and selects that related record in the master file. It's working great but getting stuck cause it doens't know what to do when it is on the last related record in the related file. How do i end that related file script so that it knows to end? Or do i put something besides END LOOP in the master file script? Here's what i have:

Master File Script:

Open "Related file"

Perform External Script #1 (listed below)

Set Field ("select", "1") (my checkbox field)

Loop

Perform Ext. Script #2 (listed below)

Set Field (Select,1)

End Loop

Related File Scripts:

#1:

Goto Layout....

Goto Field .....

Enter Find (Restore, pause)

Perform Find

Goto Related Record - (Checked "show only related files")

#2: ( This is what's looped in the master file)

Goto Record Request - Next ( so it jumps to next record in my found set in the related file)

Goto Related Record (here's where it goes back to that loop in the Master File script)

PLEASE HELP!!! smile.gif" border="0

Link to comment
Share on other sites

By my understanding you are running the script with the loop in one database (I'll call it "Main") and inside the loop you are performing an external script from another database (I'll call it "D2")

A solution to your problem could be adding a global field to the "Main" Database which I'll call "gFlag". At the end of the external script that you are running from the loop you can add...

If ( Status(CurrentFoundCount) = Status(CurrentRecordNumber) )

Set Field ( Main::gFlag , 1)

End If

Using the status commands you don't have to worry about different number of records because the actual number of what record you are on and how many records you are looking at will be returned ("real-time")

Now, in the script in the "Main" database, before the 'Loop' command add

Set Field ( gFlag , 0 )

And before the 'End Loop' command add

Exit Loop If ( gFlag = 1 )

Hope this helps.

Link to comment
Share on other sites

First off you definitly need an 'Exit Loop If' command.

On how to exit the loop, perhaps you can use a global field as a flag. If the current record is equal to the total number of records then set the global flag to something that indicates that it is finished, and then 'Exit Loop If' that field equals whatever it is that you set it to (possibly 0 for not done and 1 for done.)

Hope this helps.

Link to comment
Share on other sites

while i was typing you ansered some of my questions. Thanks. So, basically i need say Exit Loop If, but then the calculation box comes up and i can't figure out how to make it say "if the record in the related file is the last". HELP!!

Link to comment
Share on other sites

Thanks again but my total number of records will be different each time. So i can't say a particular number in the script. It depends on the found set. Is there a way to say "last record of the found set" ?? You know, i don't really get this global field thing either. I can't find a good definition of how to use that in the FM book. Maybe i do need that? I'M SO CLOSE YET SO FAR!

[ November 13, 2001: Message edited by: KLA ]

Link to comment
Share on other sites

This topic is 7284 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.