Blog Post

Small Basic Blog
2 MIN READ

Arrays in Small Basic

Ed Price's avatar
Ed Price
Former Employee
Feb 12, 2019
First published on MSDN on Jun 20, 2009

Authored by Vijaye Raji


With version 0.5, Small Basic implements native support for arrays. This is a significant change from how arrays were used in v0.4 and so I want to write about the syntax and the functionality of the new arrays.


Any variable can be used as an array – no special declaration or setup is necessary. Arrays are indexed using square brackets.


numbers[1] = "One"
numbers[2] = "Two"
TextWindow.WriteLine(numbers[1])
TextWindow.WriteLine(numbers[2])

Arrays can be indexed with either numbers or text. And you can use different types of indexers in the same array.


myarray["one"] = 1
myarray[2300] = "Two thousand three hundred"
myarray["name"] = "Vijaye"

Arrays can be copied over via simple assignment. Modifying one array doesn’t affect the other array.


first[1] = "Uno"
first[2] = "Dos"
second = first
TextWindow.WriteLine(second[2]) ' prints Dos
second[1] = "One"
TextWindow.WriteLine(second[1]) ' prints One
TextWindow.WriteLine(first[1]) ' prints Uno

The values in an array are internally maintained as a string with semicolon separated values:


person["Name"] = "Vijaye"
person["Age"] = 30
person["Address"] = "Redmond"
TextWindow.WriteLine(person)

This prints:


Name=Vijaye;Age=30;Address=Redmond;

You can remove elements in an array by setting them to an empty text.


myarray[1] = "One"
myarray[2] = "Two"
myarray[3] = "Three"
TextWindow.WriteLine(Array.GetItemCount(myarray)) ' prints 3
myarray[2] = ""
TextWindow.WriteLine(Array.GetItemCount(myarray)) ' prints 2

And finally, arrays can be multi-dimensional too


people[1]["Name"]["First"] = "Vijaye"
people[1]["Name"]["Last"] = "Raji"
people[2]["Name"]["First"] = "Carl"
people[2]["Name"]["Last"] = "Fredrickson"
TextWindow.WriteLine(people[2]["Name"])

This prints:


First=Carl;Last=Fredrickson;

Theoretically, you can have as many dimensions as you want. However, the way they are implemented internally, a two dimensional array is 2 times slower than a single dimension array, and a three dimensional array is 4 times slower than a single dimensional array and so on. So, I’d recommend not overdoing multidimensional arrays.


Published Feb 12, 2019
Version 1.0
No CommentsBe the first to comment