Sheerpower Logo
N.4  Understanding Clusters in Sheerpower

Lesson 4: Understanding Clusters in Sheerpower

Clusters in Sheerpower are powerful tools that allow you to combine multiple variables into a single named object. This feature simplifies data management, making your code clearer and easier to maintain. Clusters are especially useful in business applications where efficient data handling is crucial. In this lesson, we'll explore the basics of clusters and how to use them effectively.

1. What is a Cluster?

A cluster is a collection of related variables grouped together under a single name. These variables, also known as cluster fields or fields, can store different types of data. Clusters are similar to structs in other programming languages. Let's start by creating a simple cluster that stores information about a meal.

cluster meals: protein$, liquid$, carb$ meals->protein$ = "Eggs" meals->liquid$ = "Tea" meals->carb$ = "Toast" print "Protein: "; meals->protein$ print "Liquid: "; meals->liquid$ print "Carb: "; meals->carb$

Instructions: Click the COPY button above to paste the code into the text area, and then click RUN to execute the code. This code creates a cluster named meals with three fields: protein$, liquid$, and carb$. The program then assigns values to these fields and prints them.

2. Using a Cluster as a Template

In Sheerpower, you can use a cluster as a template to create related clusters. This is useful when you have multiple clusters that share the same structure. Let's define a cluster for meals and use it to create clusters for breakfast, lunch, and dinner.

cluster meals: protein$, liquid$, carb$ meals->protein$ = "Eggs" meals->liquid$ = "Tea" meals->carb$ = "Toast" cluster breakfast using meals breakfast->protein$ = "Eggs" breakfast->liquid$ = "Tea" breakfast->carb$ = "Toast" cluster lunch using meals lunch->protein$ = "Chicken" lunch->liquid$ = "Coffee" lunch->carb$ = "Rice" cluster dinner using meals dinner->protein$ = "Steak" dinner->liquid$ = "Wine" dinner->carb$ = "Rice" print "Breakfast: "; breakfast->protein$; ", "; breakfast->liquid$; ", "; breakfast->carb$ print "Lunch: "; lunch->protein$; ", "; lunch->liquid$; ", "; lunch->carb$ print "Dinner: "; dinner->protein$; ", "; dinner->liquid$; ", "; dinner->carb$

Instructions: Click the COPY button above to paste the code into the text area, and then click RUN to execute the code. This code creates a template cluster named meals and then uses it to define clusters for breakfast, lunch, and dinner. The values for each meal are then printed.

3. Initializing Cluster fields

You can initialize cluster fields with values when you first define the cluster. This can make your code more concise and easier to read. Let's create a cluster for a diet plan with initial values for each field.

cluster meals: protein$, liquid$, carb$ meals->protein$ = "Eggs" meals->liquid$ = "Tea" meals->carb$ = "Toast" cluster breakfast using meals breakfast->protein$ = "Eggs" breakfast->liquid$ = "Tea" breakfast->carb$ = "Toast" cluster lunch using meals lunch->protein$ = "Chicken" lunch->liquid$ = "Coffee" lunch->carb$ = "Rice" cluster dinner using meals dinner->protein$ = "Steak" dinner->liquid$ = "Wine" dinner->carb$ = "Rice" print "Breakfast: "; breakfast->protein$; ", "; breakfast->liquid$; ", "; breakfast->carb$ print "Lunch: "; lunch->protein$; ", "; lunch->liquid$; ", "; lunch->carb$ print "Dinner: "; dinner->protein$; ", "; dinner->liquid$; ", "; dinner->carb$ cluster diet: protein$ = "Steak", liquid$ = "Wine", carb$ = "Rice", calories = 2000 print "Diet plan:" print "Protein: "; diet->protein$ print "Liquid: "; diet->liquid$ print "Carb: "; diet->carb$ print "Calories: "; diet->calories

Instructions: Click the COPY button above to paste the code into the text area, and then click RUN to execute the code. This code defines a cluster named diet with initial values for its fields. The values are then printed to the screen.

4. Printing Cluster fields

Sheerpower provides an easy way to print the names and values of all fields of a cluster using the print cluster statement. This is useful for debugging and for getting a quick overview of the cluster's contents.

cluster meals: protein$, liquid$, carb$ meals->protein$ = "Eggs" meals->liquid$ = "Tea" meals->carb$ = "Toast" cluster breakfast using meals breakfast->protein$ = "Eggs" breakfast->liquid$ = "Tea" breakfast->carb$ = "Toast" cluster lunch using meals lunch->protein$ = "Chicken" lunch->liquid$ = "Coffee" lunch->carb$ = "Rice" cluster dinner using meals dinner->protein$ = "Steak" dinner->liquid$ = "Wine" dinner->carb$ = "Rice" print "Breakfast: "; breakfast->protein$; ", "; breakfast->liquid$; ", "; breakfast->carb$ print "Lunch: "; lunch->protein$; ", "; lunch->liquid$; ", "; lunch->carb$ print "Dinner: "; dinner->protein$; ", "; dinner->liquid$; ", "; dinner->carb$ cluster diet: protein$ = "Steak", liquid$ = "Wine", carb$ = "Rice", calories = 2000 print "Diet plan:" print "Protein: "; diet->protein$ print "Liquid: "; diet->liquid$ print "Carb: "; diet->carb$ print "Calories: "; diet->calories print cluster meals, list

Instructions: Click the COPY button above to paste the code into the text area, and then click RUN to execute the code. This code prints the names and values of all fields of the meals cluster.

5. Passing Clusters to Routines

When you need to pass a lot of data into a routine, using a cluster can simplify your code. Let's create a routine that calculates the total calories in a meal by passing a cluster to it.

cluster meals: protein$, liquid$, carb$ meals->protein$ = "Eggs" meals->liquid$ = "Tea" meals->carb$ = "Toast" cluster breakfast using meals breakfast->protein$ = "Eggs" breakfast->liquid$ = "Tea" breakfast->carb$ = "Toast" cluster lunch using meals lunch->protein$ = "Chicken" lunch->liquid$ = "Coffee" lunch->carb$ = "Rice" cluster dinner using meals dinner->protein$ = "Steak" dinner->liquid$ = "Wine" dinner->carb$ = "Rice" print "Breakfast: "; breakfast->protein$; ", "; breakfast->liquid$; ", "; breakfast->carb$ print "Lunch: "; lunch->protein$; ", "; lunch->liquid$; ", "; lunch->carb$ print "Dinner: "; dinner->protein$; ", "; dinner->liquid$; ", "; dinner->carb$ cluster diet: protein$ = "Steak", liquid$ = "Wine", carb$ = "Rice", calories = 2000 print "Diet plan:" print "Protein: "; diet->protein$ print "Liquid: "; diet->liquid$ print "Carb: "; diet->carb$ print "Calories: "; diet->calories print cluster meals, list cluster meal: protein$, liquid$, carb$, calories meal->protein$ = "Chicken" meal->liquid$ = "Juice" meal->carb$ = "Bread" meal->calories = 600 calculate_calories with meal routine calculate_calories with meal print "Total calories in the meal: "; meal->calories end routine

Instructions: Click the COPY button above to paste the code into the text area, and then click RUN to execute the code. This code creates a cluster named meal and passes it to the calculate_calories routine, which then prints the total calories.

Conclusion

In this lesson, you've learned the basics of working with clusters in Sheerpower, including how to create clusters, use them as templates, initialize fields, print cluster fields, and pass clusters to routines. Clusters are a powerful tool for managing complex data in an organized and efficient way. Keep practicing to master clusters 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