Skip to end of metadata
Go to start of metadata

Ever wondered what it’s like to not have to write hundreds of lines of code to complete repetitive tasks that seem like they sho

uld be easier? Well, let me introduce – Structured For Loops!

If you’ve worked with other programming languages you might be familiar with the concept of creating loops. The basic idea is that the system needs a way of repeating tasks over and over until a process finishes.

Step-by-step guide

In many OLIE scripts, this was completed with a simple for loop as shown below:

 Let count = 0
 For 10
      Let count = count + 1
      Tell "Counting : " count
 EndFor

This is a good one-size-fits-all approach to some tasks, but what if you have dynamic data that may vary in size? Shouldn’t there be a more elegant solution?

There is, a Structured For Loop

Completing Repetitive Tasks

Let’s say that we have a screen that we want to check or scrape for values on lines 3 to 20. I’ll use the example of a menu screen to demonstrate, but in a real situation this could be used on a customer record to collect specific information, build reports, or search multiple lines or pages for specific lines of text.

In a traditional sense we might do this by writing the following code:

 :ScrapeScreen
 CreateList ReportBuild
 Copy LineString SCREEN row 3 80
 PutListMember ReportBuild LineString
 Copy LineString SCREEN row 4 80
 PutListMember ReportBuild LineString
 Copy LineString SCREEN row 5 80
 PutListMember ReportBuild LineString
 Copy LineString SCREEN row 6 80
 PutListMember ReportBuild LineString
 Copy LineString SCREEN row 7 80
 PutListMember ReportBuild LineString
 Copy LineString SCREEN row 8 80
 PutListMember ReportBuild LineString
 Copy LineString SCREEN row 9 80
 PutListMember ReportBuild LineString
 Copy LineString SCREEN row 10 80
 PutListMember ReportBuild LineString
 Copy LineString SCREEN row 11 80
 PutListMember ReportBuild LineString
 Copy LineString SCREEN row 12 80
 PutListMember ReportBuild LineString
 Copy LineString SCREEN row 13 80
 PutListMember ReportBuild LineString
 Copy LineString SCREEN row 14 80
 PutListMember ReportBuild LineString
 Copy LineString SCREEN row 15 80
 PutListMember ReportBuild LineString
 Copy LineString SCREEN row 16 80
 Copy LineString SCREEN row 17 80
 PutListMember ReportBuild LineString
 Copy LineString SCREEN row 18 80
 PutListMember ReportBuild LineString
 Copy LineString SCREEN row 19 80
 PutListMember ReportBuild LineString
 Copy LineString SCREEN row 20 80
 PutListMember ReportBuild LineString
 [Change Screen]
 GoTo ScrapeScreen

That’s over 35 lines of code to simply place items onto a list! Of course even in Olie this wasn’t the best way of accomplishing that task.

Also, if we want to check for a value on the list we would then have to create the code to do that as well.

A New Perspective

Instead of the code above, we can use a Structured For loop to accomplish the same task. Let’s take a look at the code below:

 CreateList MenuOptions
 For row = 3 TO 20
      Copy LineString SCREEN row 3 80
      PutListMember MenuOptions LineString
 Next row

That’s it! 5 lines of code to accomplish the same task. So how does it work?

  1. First we create a list using the CreateList command. This gives us somewhere to place our data if we intend to use it later.

      CreateList [List Name]


  2. Next, we initialize our Structured For loop by writing four separate parts - the “For” command, a variable that contains our incrementing value, the keyword “To”, and the numeric value or variable that will tell the system where to end

    • We can find the needed row information (ex. 3 and 20) by clicking directly on the rumba terminal session and reading the “Y” coordinate located at the top right of the screen  . The Y value corresponds to the row, and the X value corresponds to the column. Do this for both the top and bottom rows you would like to retrieve
  3. Next we include whatever code we wish to complete during each iteration. In this case we are simply putting each line into a list that we can utilize at a later point.

    Copy LineString Screen row 3 80
    PutListMember MenuOptions LineString


  4. Lastly we have to tell EMUE that we want to move on to the next row with the code below. One thing to note is that when we are working with one For loop we only need to use the keyword “Next” to increment to the next unit (in this example, rows). When working with multiple for loops, we can provide the name of the incremented unit (ex. Rows) to make the statement clearer to someone reading the script; however, if this value is omitted EMUE will still increment the proper values.

Next Row

Putting It All Together

Now that we have a for loop in place, you can do many repetitive tasks that would otherwise require many lines of code. Here’s an example that checks for a menu listing and creates a list of all menu items at the same time.

After running this script we would not only have a list of items, but we would also know the option number of the menu items that we were looking for.

Two other commands can be used to control how Structured For loops operate. “Continue” pictured above, will stop the current iteration early, and proceed on to the next one. In the example above, once we have found the item we are looking for we want to continue looping through the rest of the values until we are finished.

The other command that is not pictured is the “ExitFor” command. Similar to the “Break” command used in simple for loops, ExitFor will immediately exit all for loops (even nested loops). Since it will exit all loops it should be used carefully.

 

Structured For Syntax

For <index> = <start> To <end> [Step <step>]

one or more statements to process

Next <index>

Where:

<index> is a variable that contains an integer

       Note: the index variable does not need to be initialized ahead of time as it will automatically be assigned the value of <start>.

<start> is an integer or a variable containing an integer

<end> is an integer or a variable containing an integer

<step> is an integer or a variable containing an integer