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:
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:
- Fixed-Size (`dim array(10)`): When you know the
exact size upfront.
- Resizable (`redim array(20)`): When you need
to expand an array later while keeping its data.
- 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!
(Show/Hide Sheerpower Arrays Takeaways)
Sheerpower Arrays Takeaways
-
DIM creates arrays using zero-based indexing, so dim a(5)
produces six elements: indices 0 through 5.
-
Array elements are accessed and assigned using parentheses,
making each index act as its own variable slot.
-
Arrays integrate naturally with FOR/NEXT loops, enabling simple
iteration across elements in numeric order.
-
Multi-dimensional arrays are created by adding more index
ranges, allowing structured data layouts such as grids.
-
Arrays provide a compact, organized way to store related values
and support efficient batch processing.