8.7 LOOP

Other than the looping operators, such as <LOOP> , <SQL> , or <fetch> , there is no way to access the individual members of a variable. This is consistent with Vortex' overall philosophy of making common tasks easier; other languages bog down with multi-statement for loops, counter variables, and end-of-loop checks to manage arrays.

The <LOOP> statement, which we used in the previous example, is worth a closer look:


  <SCRIPT LANGUAGE=vortex>

  <A NAME=main>
    <$things= apple hair   lemon  leaf  ocean grape  soil  night>
    <$colors= red   orange yellow green blue  purple brown black>
  
    <LOOP SKIP=2 MAX=4 $colors $things>
       Color #$loop is $things $colors <BR>  
    </LOOP> 
    The next color is #$next
  </A>

  </SCRIPT>

In this example we have two parallel lists, $things and $colors . We iterate over just 4 members of the list (MAX=4 ) - after skipping the first 2 (SKIP=2 ).

If we run this script, we see the results:

Netscape: Untitled

File Edit View Go Communicator
Location:
Color #2 is lemon yellow
Color #3 is leaf green
Color #4 is ocean blue
Color #5 is grape purple
The next color is #6

We skipped the first 2 values of each variable, and printed the next 4.

Note the special variables $loop and $next . $loop is set each iteration to the current list index (counting from 0). $next is set to the next value's index (or to think of it another way, the current value's, but counting from 1).

Back: Variable Values - Continued Next: Variable Types
Copyright © 2024 Thunderstone Software LLC. All rights reserved.