Sheerpower Logo
D.2  Cluster Arrays (collections)

sheerpower Clusters

Cluster Arrays (Collections)

A cluster array in sheerpower is similar to an in-memory spreadsheet, containing rows and columns of data. These clusters can efficiently store and manipulate data, making them ideal for handling information like spreadsheets.

sheerpower includes a rich set of features that allows you to input spreadsheet files directly into clusters and perform various operations such as sorting, including, excluding, and searching—much like you would in a database.

Main Features of sheerpower Cluster Arrays

  • Dynamic Key-Value Manipulation: sheerpower clusters support dynamic key-value manipulation natively. All fields in a cluster are mutable, which allows for flexible data handling.
  • Multiple Key Fields: Clusters can have multiple key fields, which can be dynamically created as needed.
  • Duplicate Values: Clusters allow for duplicate values in their fields, providing flexibility in data storage.
  • Dynamic Resizing: Cluster arrays in sheerpower are dynamically sized, meaning they automatically adjust their size as rows are added or removed. This dynamic resizing is handled by sheerpower, ensuring efficient memory management without requiring manual intervention by the developer.
  • Performance-Oriented: sheerpower clusters are optimized for performance, particularly in environments with abundant RAM. They utilize internal caching and hinting to reduce overhead, ensuring high efficiency.

Creating a Cluster Array to Hold Student Information

Let's create a cluster array called STUDENT to hold information about students. Each row in this cluster will represent a different student, and we will track each student's name, age, and grade level.

cluster student: name$, age, level

The most common method to add data to a cluster is by adding 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 = 12

In this case, a new cluster row is established, storing information about the student "Joan Ark," who is 18 years old and in grade level 12.

Adding More Students

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 = 10

The row added last is considered the current row. For example, after the above code, row three (Desmond Jones) is current.

print student->name$ // "Desmond Jones" print student->age // 15 print student->level // 10

To print information about row one, you first make row one current:

set cluster student: row 1 print student->name$ // Joan Ark

Working with Cluster Arrays

To find out how many rows are in a cluster, use the SIZE() function:

print size(student) // 3

To ask which row is current, use the ASK CLUSTER statement:

set cluster student: row 2 ask cluster student: row x print x // 2

Iterating Through Cluster Arrays

To operate on each row in a cluster array, use the COLLECT and END COLLECT statements. For example, to print the name, age, and grade level of each student and calculate their average age:

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/counter

Sorting and Filtering Data

sheerpower makes it easy to filter and sort data within a cluster array. Here’s how you can do it:

  • First, collect the student records using the collect and end collect block.
  • Within this block, you can filter data using include and exclude statements. For example, you might filter to include only students over 16 years old.
  • Any sort statements must be placed after the filtering. The default sort order is ascending.
  • To sort in descending order, use the sort descending statement.
  • You can include multiple include, exclude, and sort statements within the collect block to refine your data further.

Once the data is collected and sorted, you can iterate through the records using the for each statement:

To include only students older than 16:

collect cluster student include student->age > 16 sort by student->name$ end collect for each student print student->name$; ' '; student->age next student

Searching Cluster Arrays

Use the FINDROW() function to search a cluster array for specific data:

print findrow(student->name$, "Joan Ark") // 1

The FINDROW() function is highly optimized, capable of performing over 10 million searches per second, making it ideal for tasks requiring fast lookups.


In sheerpower, any fields in a cluster that are specified in the findrow() function are automatically converted and maintained as key fields from that point forward. These key fields are dynamically managed by sheerpower and do not require unique values. Additionally, the content of key fields can be changed at any time, offering flexibility in how data is organized and accessed within a cluster. This feature allows developers to efficiently search and manipulate data without the need for predefined key fields. Additionally, any data type can be used as a key.

Hide Description

    

       


      

Enter or modify the code below, and then click on RUN

Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use.
Wide screen