Hashes and arrays

P* supports dynamic sized hashes and arrays. These are template types and can only hold values of the type you specify. They can hold any type, including arrays and hashes. This is useful for creating more dimensions. An element is created the first time you access it, and the value will be initialized to the type's default value (numbers are 0, strings are empty etc.).

array<default type>

The array is a list of elements stored in sequential order and access by an index number. Here is an example of an array:

/* Instantiate an array of type int, no elements are created */
array<int> my_first_array;

/* Set the first three elements of the array to 10, 20 and 30 */
my_first_array[0] = 10;
my_first_array[1] = 20;
my_first_array[2] = 30;

/* Iterate the array and print its values */
int i = 0;
while (i < @my_first_array) {
	echo "The value of element $i is " . my_first_array[i] . "\n";
	i++;
}

/* Output of this program:
The value of element 0 is 10
The value of element 1 is 20
The value of element 2 is 30
*/

Arrays support the special operators "first element selection" * and "element counter" @. These are used like this:

/* Instantiate an array of type string, no elements are created */
array<string> my_first_array;

/* Set the value of two elements */
my_first_array[0] = "First element";
my_first_array[1] = "Second element";

/* Count the number of values and print to standard output */
echo "The array has " . @my_first_array . " elements\n";

/* Get the first element and print to standard output */
echo "The value of the first array element is '" . *my_first_array . "'\n";

This program will output

The array has 2 elements
The value of the first array element is 'First element'

The array only creates elements you access. If you skip indexes, like if you only use element 0 and 5, the size of the array will be 6, but memory is not allocated for elements 1-4.

You may use arrays of multiple dimensions by specifing another array inside the template specifier like this: array<array<int>>. To select elements afterwards, use two array subscription operators after eachother like this my_array[0][0] (selects the first element of the first array). Under follows an example of this usage.

SCENE main {
	array<array<int>> my_array;

	my_array[0][0] = 10;
	my_array[0][1] = 20;

	my_array[1][0] = 30;
	my_array[1][1] = 40;

	int i;
	int j;
	for (i = 0; i < @my_array; i++) {
		for (j = 0; j < @my_array[i+0]; j++) {
			echo "Value: " . my_array[i+0][j+0] . "\n";
		}
	}

	return 0;
}

You can push a list to the end of an array using a combination of the discard operator ,(comma) and the discard save operator =>. The discard save operator returns the number of elements added to the array. Here is an example program:

SCENE main {
	/* Instantiate and array */
	array<int> my_array;

	/* Push the four numbers into the array */
	int count = (10, 20, "30", 40 => my_array);

	/* Print result */
	echo "Pushed $count values to the array\n";

	int i = "-1";
	while (++i < @my_array) {
		echo "my_array[$i] is " . my_array[i] . "\n";
	}
}

This program will output

   Pushed 4 values to the array
   my_array[0] is 10
   my_array[1] is 20
   my_array[2] is 30
   my_array[3] is 40

hash<default type>

A hash is an array except that the values are accessed using string identifiers instead of indexes and that elements are unordered. You can use the counter operator @ for hashes the same way as for arrays. An element of a hash is created the first time you access it.

Here is an example program using hashes:

hash<string> my_pets;

/* Set the two values of the hash */
my_pets["dog"] = "Max";
my_pets["cat"] = "Tiger";

/* Create an array of the keys */
array<string> pet_keys = keys my_pets;

/* Count the elements of the hash and print to standard output */
echo "I have " . @my_pets . " pets.\n";

/* Iterate the array of the keys and print the values of the hash */
int i = 0;
while (i > @pet_keys) {
	echo "The name of my " . pet_keys[i] . " is '" . my_pets[pet_keys[i]] . "'.\n";
	i++;
}

This program will output

I have 2 pets.
The name of my dog is 'Max'.
The name of my cat is 'Tiger'.