Final words

So there you are. Arrays in Axe.

Before we leave you to coding in peace, there's one last trick we're here to teach you: how to make arrays whose elements are arrays in their own right, or arrays of arrays.

Arrays in arrays? What's the point of that?

Surprisingly, you can do a lot with them. For example, say you have a puzzle game where each level has a fixed set of enemies. Instead of making a gigantic if-elseif-elseif-elseif... structure, you can put the enemy data for each level in an array, then organize that whole thing into an array.

But here's the problem: How do you store an array of elements that aren't a fixed size? The real problem comes in parsing it—how could you loop through a list whose elements could be any number of bytes long?

Like with any varying array, the solution comes in embracing the pointers. By that we mean simply that you don't have to put the structures themselves into the array; just point to them. Here's an example.

First, create the raw data you're going to put into an array.

PROGRAM:A
:.B
:"The G"→Str000
:"ame i"→Str001
:"s nea"→Str002
:"r. Pr"→Str010
:"epare"→Str011

Here are the first-level arrays:

:Data(3,Str000r,Str001r,Str002r)→Str00
:Data(2,Str010r,Str011r)→Str00

Notice we use Data( with each pointer followed by an r (since pointers are always two bytes). The 3 and 2 in the beginning denote the number of elements in that array, since this value can change. Now it's the arrays' turn to be referenced from an array, in much the same way:

:Data(Str00+1r,Str01+1r)→Str0

The increments are there to offset the length prefixes on those arrays.

To do something with this, let's loop through the array and all its arrays to print whatever is stored there:

:ClrHome
:For(I,0,1)
:{{I*2+Str0}r→K-1)-1→L
:For(J,0,L)
:Disp {J*2+K}r
:End
:End

With that, let's call it a day. Enjoy your newfound powers and happy coding!

+ Tweet