|D.2 Cluster Arrays|
cluster student: name$, age, levelThe most common method to put data into a cluster is to add a new row to the end of the cluster using the ADD CLUSTER statement.
add cluster student student->name$ = "Joan Ark" student->age = 18 student->level = 12The add cluster establishes a new cluster row. In this case, since the cluster was empty, this is row one. The next lines of code store information into each variable (think "column") of that cluster row. So, the name of the first student is "Joan Ark", she is 18 years old, and is at grade level 12.
add cluster student student->name$ = "John Smith" student->age = 16 student->level = 10 add cluster student student->name$ = "Desmond Jones" student->age = 15 student->level = 10Directly after adding a new row, that row is said to be CURRENT. In this example, row three would be current.<
print student->name$ // "Desmond Jones" print student->age // 15 print student->level // 10In order to print information about row one, you would first make row one current, and then access the variables in that row.
set cluster student: row 1 print student->name$ // Joan ArkTo find out how many rows are in a cluster, use the SIZE() function:
print size(student) // 3To ask which row is current, use the ask cluster statement:
set cluster student: row 2 ask cluster student: row x print x // 2When working with cluster arrays, we typically want to operate on each row, one at a time. To do this we can use the COLLECT and END COLLECT statements.
ages = 0 counter = 0 collect cluster student print student->name$, student->age, student->level ages = ages + student->age counter++ end collect print 'The average age is '; ages/counterThe COLLECT/END COLLECT iterates through each row of a cluster. While doing so, it creates a COLLECTION of rows. A collection can be a subset of the entire cluster array and can be sorted by various criteria. To iterate through a collection, use the FOR/NEXT statements. Let's sort the students by their name and print out the sorted list:
collect cluster student sort by student->name$ end collect for each student print student->name$; ' '; student->age next studentWe can also include or exclude students:
collect cluster student include student->age > 16 sort by student->name$ end collect for each student print student->name$; ' '; student->age next studentAny number of include, exclude, or sort statements can be used on a cluster array.
print findrow(student->name$, "Joan Ark") // 1The findrow() function is highly optimized. Over five million searches can be done per second. This makes findrow() ideal for tasks that require fast lookups. If the search is successful, the cluster array row is now current.
Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use.