Sheerpower Logo

B.5  Arrays


Understanding Traditional Arrays

Arrays are fundamental data structures that let you store a list of items in a single variable. Think of them as a set of numbered slots, each holding a value. In SheerPower, arrays are a simple way to manage lists, but it's also important to know when a more powerful tool, like a Cluster Array, is a better choice.

1. The Simple Case: Fixed-Size Arrays

If you know exactly how many items you need to store, you can create a fixed-size array using the dim statement.

! Create an array with 5 slots for names dim names$(5) ! Assign values to each slot using its index names$(1) = "Alice" names$(2) = "Bob" names$(3) = "Charlie" print "The second name is: "; names$(2)

This creates an array that looks like this:

1
"Alice"
2
"Bob"
3
"Charlie"
4
 
5
 

2. Needing More Space: Resizing with `redim`

What if you create an array and later realize you need more slots? You can use the redim statement to expand it. Importantly, redim preserves all the existing data in the array.

! We have our array of 5 names, with 3 filled redim names$(10) ! Resize the array to have 10 slots ! The original data is still there print "The second name is still: "; names$(2) ! Now we can add a new name in a new slot names$(6) = "Frank" print "The sixth name is: "; names$(6)

Visually, the `redim` operation does this:

names$(5)
redim names$(10)
names$(10)

3. The Ultimate Flexibility: Dynamic Arrays

What if you don't know the size at all and want to add items one by one? SheerPower has a special syntax for this: creating an array with a size of zero.

! Create a dynamic array for a shopping list dim shopping_list$(0)

To add an item, you use a unique syntax: you assign the new value to index 0. This special operation appends the item to the end of the array, making it grow automatically.

print "Building shopping list..." shopping_list$(0) = "Milk" ! Appends "Milk", size is now 1 shopping_list$(0) = "Bread" ! Appends "Bread", size is now 2 shopping_list$(0) = "Cheese" ! Appends "Cheese", size is now 3 print "My list has "; size(shopping_list$); " items." print "The second item is: "; shopping_list$(2)

4. The Next Step: When to Use Cluster Arrays Instead

Traditional arrays are great for simple lists of single values. However, most business data is structured. For example, instead of just a list of product names, you usually need to store the product's name, its price, and its quantity together.

For this kind of structured data, the Cluster Array is the preferred and more powerful tool in SheerPower. Think of it as a spreadsheet in memory, where each row is a complete record.

Recommendation: While it's important to understand traditional arrays, for most modern data handling tasks in SheerPower, you should use a Cluster Array.
Summary: You've learned the three ways to work with traditional arrays:
  1. Fixed-Size (`dim array(10)`): When you know the exact size upfront.
  2. Resizable (`redim array(20)`): When you need to expand an array later while keeping its data.
  3. Dynamic (`dim array(0)`): When you need to add items one by one.
For anything more complex than a simple list, explore Cluster Arrays next!
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.