Xbasic

Iterating Through Arrays, Collections, and Lists

Description

You will frequently find yourself working with groups of data elements outside of a table. Xbasic provides tools for working with data in three formats:

  • Lists 

    Data Type: Character, 1 Dimension, Data elements typically separated by CR an LF characters or other distinct characters. Refer tofor more information.

  • Arrays 

    Data Type: Any, Multiple dimensions, Alpha Anywhere organizes data elements. You find data by specifying one or more index values.

  • Collections 

    Data Type: Any, 1 Dimension, Alpha Anywhere organizes data elements. You find and retrieve data by using a key value.

Iterating Through Arrays 

The simplest way to reference the data elements in an array is to specify index (or subscript values). You can read or write data elements with a simple assignment operation (=).

dim chessboard[8,8] as P
dim i as N
dim k as N
dim count as N = 0
for i = 1 to 8
    for k = 1 to 8
        count = count + 1
        if (mod(count,2) = 1) then
            chessboard[i,k].color = "Red"
        else
            chessboard[i,k].color = "Black"
        end if
    next k
next i

If you do not know the size of an array, the size method will provide it. This example returns the size of the second dimension of the chessboard variable.

dim chessboard[8,8] as P
? chessboard.size(2)
= 8

The *FOR_EACH() function is useful for looping through an array when you do not know its size.

dim va[5] as P
  
va[1].firstname = "Larry" 
 va[1].lastname = "Jones" 
 va[2].firstname = "Sue" 
 va[2].lastname = "Brown" 
 va[3].firstname = "Toby" 
 va[3].lastname = "Smith" 
 va[4].firstname = "Bob" 
 va[4].lastname = "Saget" 
 va[5].firstname = "Mary" 
 va[5].lastname = "Johnson" 


 vList = *for_each(x,"Dear "+x.firstname + " " + x.lastname,va) 
 ? vlist 
 = Dear Larry Jones 
 Dear Sue Brown 
 Dear Toby Smith 
 Dear Bob Saget 
 Dear Mary Johnson

The following script shows how to read the data elements in a collection. First, create a collection.

dim mycollection as u
mycollection.SET("FJ","Fred Jones")
mycollection.SET("BB","Bryan Boyd")
mycollection.SET("KL","Kim Lee")
mycollection.SET("KB","Karen Boyd")
mycollection.SET("EL","Erica Loyd")

Now, read and display each value. Working with a collection is similar to working with an indexed table.

dim k as N
dim i as N = 1
dim keyval as C
dim currentval as C
k = mycollection.size()
keyval = mycollection.first()
currentval = mycollection.get(keyval)
ui_msg_box("", currentval)
while (i < k)
    keyval = mycollection.next(keyval)
    currentval = mycollection.get(keyval)
    i = i + 1
    ui_msg_box("", currentval)
end while

Use the <COLLECTION>.SET() method to change a data element in a collection. If the specified key value exists, the method changes the data element. If the key does not exist, the method adds a new data element to the end of the collection.

dim currentval as C
dim newval as C
i = 1
keyval = mycollection.first()
currentval = mycollection.get(keyval)
newval = currentval + " new"
mycollection.set(keyval, newval)
ui_msg_box("",mycollection.get(keyval))
while (i < k)
    keyval = mycollection.next(keyval)
    currentval = mycollection.get(keyval)
    newval = currentval + " new"
    mycollection.set(keyval, newval)
    i = i + 1
    ui_msg_box("", mycollection.get(keyval))
end while

The following script shows how to use *FOR_EACH() to step through the elements in a CR-LF delimiter list.

dim lst1 as C
dim lst2 as C
lst1 = cstates("S"+ crlf())
lst2 = *for_each(x, left(x,3), lst)
? lst1
= Alabama
Alaska
Arizona
Arkansas
California
Colorado
...
? lst2
= Ala
Ala
Ari
Ark
Cal
Col
...

The following script uses a FOR EACH ... NEXT to step through elements of a list.

dim lst1 as C
dim lst2 as C
lst1 = cstates("S"+ crlf())
for each x in lst1
    lst2 = lst2 + left(x,3) + crlf()
next
? lst2
= Ala
Ala
Ari
Ark
Cal
Col
...

The following script shows how to use LINE_COUNT() to determine the size of a list and extract its elements with WORD().

dim lst1 as C
dim lst2 as C
dim line as C
dim k as N
lst1 = cstates("S"+ crlf())
k = line_count(lst1)
for i = 1 to k
    line = word(lst1, i, crlf())
    lst2 = lst2 + left(line,3) + crlf()
next i
? lst2
= Ala
Ala
Ari
Ark
Cal
Col
...

See Also