Monday, December 19, 2011

Arrays in RAPTOR

What is an array?
It's a set of values (numbers or strings) with one name.
You can also think of an array as a single variable with multiple slots for values.

Array Terms and Indexing
Arrays are found in all programming languages, including RAPTOR. The individual values of an array are called the elements of the array. Elements are assigned (and retrieved) by their index number inside square brackets [ ]. In RAPTOR, the first element is index 1, the second element is index 2, and so on.

* Unlike RAPTOR, in most programming languages array indexing starts at 0, not 1.
Retrieviing Array Elements
To display the value of an array element, just specify the element's index inside the square brackets [ ] as shown here.
This output would generate "Test 2 score = 87".

You can also use a numeric variable inside the square brackets. The two RAPTOR symbols on the right here would generate the same output as above. In this example, the value of the index variable was set with an Assignment symbol. An Input symbol could also be used to set the value of the index variable if your user needs to select the desired array element.

Processing Arrays With Loops
A loop can process an entire array by cycling through each element of the array from start to finish. The key is to use the loop control variable (count in our example here) inside the square brackets. Set the initial value of count to 1 and increment count by 1 in every loop cycle and your loop will run through the array, element by element. To enable processing an entire array of any size, RAPTOR has the length_of( ) function. This function holds the size of the array named inside the ( ). Use it, as shown in the subchart here, to detect the end of the array and stop the loop.
NOTE : This subchart's loop outputs the entire array no matter how many elements it holds.

Arrays of Strings

The example above shows an array in which all of the elements are numbers. You can also create arrays in which the elements are quoted strings. In fact, in RAPTOR and some scripting languages, you can mix numbers and strings in the same array.