Sheerpower Logo
N.5  Cluster Arrays Collections

Lesson 5: Working with Cluster Arrays in Sheerpower

Cluster arrays in Sheerpower are powerful tools that function similarly to in-memory spreadsheets. They allow you to efficiently store, manipulate, and retrieve data in rows and columns, making them ideal for handling information like spreadsheets. In this lesson, we'll explore the main features of cluster arrays and how to use them in Sheerpower.

1. Creating a Cluster Array

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

cluster student: name$, age, level add cluster student student->name$ = "Joan Ark" student->age = 18 student->level = 12 print student->name$ print student->age print student->level

Instructions: Click the COPY button above to paste the code into the text area, and then click RUN to execute the code.

2. Adding More Students

We can add more students to the cluster by adding new rows. Each row stores information about a different student.

cluster student: name$, age, level add cluster student student->name$ = "Joan Ark" student->age = 18 student->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 = 10 print "Current student: "; student->name$

Instructions: Click the COPY button above to paste the code into the text area, and then click RUN to execute the code.

3. Working with Cluster Arrays

To work with cluster arrays effectively, you need to know how to check the number of rows, which row is current, and how to switch between rows.

cluster student: name$, age, level add cluster student student->name$ = "Joan Ark" student->age = 18 student->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 = 10 print "Total students: "; size(student) set cluster student: row 1 print "First student: "; student->name$

Instructions: Click the COPY button above to paste the code into the text area, and then click RUN to execute the code.

4. Iterating Through Cluster Arrays

To operate on each row in a cluster array, you can use the COLLECT and END COLLECT statements. Let's print the name, age, and grade level of each student and calculate their average age.

cluster student: name$, age, level add cluster student student->name$ = "Joan Ark" student->age = 18 student->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 = 10 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

Instructions: Click the COPY button above to paste the code into the text area, and then click RUN to execute the code.

5. Sorting and Filtering Data

Cluster arrays allow you to sort and filter data efficiently. Let's sort the students by their name and then include only those older than 16.

cluster student: name$, age, level add cluster student student->name$ = "Joan Ark" student->age = 18 student->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 = 10 collect cluster student sort by student->name$ end collect for each student print student->name$; ' '; student->age next student print collect cluster student include student->age > 16 sort by student->name$ end collect for each student print student->name$; ' '; student->age next student

Instructions: Click the COPY button above to paste the code into the text area, and then click RUN to execute the code.

6. Searching Cluster Arrays

Use the FINDROW() function to search for specific data in a cluster array. This function is highly optimized and can perform millions of searches per second.

cluster student: name$, age, level add cluster student student->name$ = "Joan Ark" student->age = 18 student->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 = 10 row = findrow(student->name$, "Joan Ark") if row > 0 then set cluster student: row row print "Found student: "; student->name$ else print "Student not found." end if

Instructions: Click the COPY button above to paste the code into the text area, and then click RUN to execute the code.

Conclusion

In this lesson, you've learned how to work with cluster arrays in Sheerpower, including how to create cluster arrays, add rows, iterate through them, sort and filter data, and search for specific information. Cluster arrays are a powerful feature in Sheerpower, offering dynamic and efficient data management. Keep practicing to master cluster arrays and enhance your Sheerpower programming skills!

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