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.

Note: Cluster arrays can be extremely large. If your computer has sufficient RAM and pagefile space, clusters can support up to one billion rows with key lookup speeds of millions per second.

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 (changeable), 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.

Note: There are two efficient methods for deleting added rows from a cluster. See Reset cluster and delete from cluster for details. To remove all rows from a cluster — commonly used when reloading data — use: delete from cluster xxx: all where xxx is the cluster name.

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.


Note:

In SheerPower, when you use the findrow() function with specific fields in a cluster, those fields are automatically optimized and treated as key fields. These key fields are managed dynamically, meaning:

  • No Unique Value Requirement: The values in these key fields do not need to be unique. Multiple rows can have the same key field value.
  • Editable Fields: You can modify the content of these key fields at any time without restrictions, providing flexibility in organizing and accessing your data.
  • Automatic Optimization: SheerPower automatically handles the optimization of these fields, so developers do not need to define or manage key fields manually.
  • Flexible Data Types: Any data type, whether numeric or string, can be used as a key field.

This all enables efficient searching and manipulation of data in clusters without requiring developers to predefine or rigidly maintain key fields.

Summary: Cluster Arrays in SheerPower

Cluster arrays in SheerPower act like in-memory spreadsheets, organizing data into rows and columns. They simplify data management with features like dynamic resizing, key-value manipulation, and efficient searching. Cluster arrays can be extremely large. If your computer has sufficient RAM and pagefile space, clusters can support up to one billion rows with key lookup speeds of millions per second.

Key Features:

  • Dynamic Resizing: Automatically adjusts as rows are added or removed.
  • Fast Searching: Supports millions of searches per second with findrow().
  • Sorting and Filtering: Built-in tools for refining data with include, exclude, and sort options.

Cluster arrays combine flexibility and performance, making them essential for efficient data handling in SheerPower applications.

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.