The world of Arrays

PPL has a very strong array system. Just like a good programming language PPL offers multi-dimensional arrays but arrays in PPL can store about anything. There is a big difference.

Arrays in PPL are like C arrays, the base index is 0 and not 1. The first element of an array is always 0 and the last is the size of array - 1.

You can store numbers or strings in arrays. Strings are a little special since they are stored as pointers only. You can have strings of any size in each element of an array. Since strings are stored as pointers you will need to convert them to strings in order to use them with functions in PPL. The good news is that PPL offers an easy-to-use operator that will do just that. The @ operator must be used with an expression (usually a variable) and will convert the result value to the string pointed by the pointer value.

Dim (a$, 10);  // Creates an array of 10 elements
a$[0] = 10.4567;  // Set first array element to value of 10.
a$[1] = "HELLO";  // Set second array element to
the pointer of string "HELLO".
ShowMessage (a$[0]);  // Shows 10.4567
ShowMessage (a$[1]);  // Shows the pointer value of
the string and not the string itself.
ShowMessage (@a$[1]);  // Shows "HELLO".
ShowMessage (@(a$[1] + 2));  // Shows "LLO";

There is way to obtain the size of the array by using the sizeof() function:

ShowMessage (sizeof(a$));  // Shows 80. 10 elements * 8 bytes each.

Remember that arrays uses double type values to store eveything? Double type values are 8 bytes long.

Multi-dimensional arrays are created and uses the following way:

Dim (a$, 10, 10, 10);
a$[1,1,2] = 293.42;

You can also create arrays using your own element type.

To get an array of bytes:

SDIM (a$, TBYTE, 20);
&a$ = "ARRAY OF BYTES";
ShowMessage(a$[3]);  // Shows 65.
ShowMessage(chr(a$[3])); // Shows "A".

What about an array of integer values?

SDIM(a$, TINT, 10, 10);
a$[5,5] = 1023;
a$[0,0] = 1983;

How do you iterate through an array?

There are multiple ways to iterate through a list. The first and easiest is to use the For loop.

Dim(a$, 10, 10);
For (x$, 0, 9)
For (y$, 0, 9)
a$[x$, y$] = x$ * y$;
end;
end;

The second is to use the ForEach.

Dim(a$, 5, 5);
i$ = 0;
ForEach (a$, s$);
s$ = i$++;
end;

Another way is to use the While or the Repeat loops:

Dim (a$, 10);
x$ = 0;
i$ = 0;
while (x$ < 10)
a$[x$] = i$++;
end;
x$ = 0;
i$ = 0;
repeat
a$[x$] = i$++;
until (x$ >= 10);

Remember that arrays can be used to store Structs and Objects. We will see in future tutorials how to do this.