Skip to main content

Arrays

ObjectScript provides multidimensional arrays, which you reference using subscripts. But arrays in ObjectScript are different, and more flexible, than regular arrays. First, just like regular, unsubscripted variables, you don't need to declare arrays, no matter the size or number of dimensions. Arrays are sparse; no space is reserved ahead of time, and no space is used until it's needed. As an example, consider an array used to keep track of the positions of pieces for a game of checkers. A checkerboard is 8x8. In other languages, an array Board(8, 8) would use 64 memory locations, even though no more than 24 positions are ever occupied by checkers.

Terminal


USER>set a(3, 7) = 4 + 2

USER>write a(3, 7)
6
USER>
Copy code to clipboard

Two-dimensional array A, in a 3-by-3 grid, from A(1,1) through A(3,3)

You can picture arrays as lists or tables, as in the simple 3x3 array A shown here. But in order to understand the additional flexibility ObjectScript arrays give you, you should learn to picture them as ordered trees. On the next few pages, you'll see an animated example of an array as an ordered tree, and this will also serve to introduce you to the other capabilities of arrays.

FeedbackOpens in a new window