Before Arrays: 7. Oversimplified Anchors — The Bookkeeping Principle 🟢 (Creating a Line in VEX)

TL;DR — VEX Arrays

Function / Operation What it does Example Result / Notes
Declare an array Creates an empty array variable float values[]; Empty float array
Initialize an array Creates an array with values float values[] = {1, 2, 3}; {1, 2, 3}
array() Creates an array using expressions or variables float a[] = array(val, 2.2); {val, 2.2}
push() Adds an item to the end push(values, 4); Adds 4
pop(array) Removes and returns the last item pop(values) Removes the last item
pop(array, index) Removes an item by index pop(values, 1) Removes index 1
removeindex() Removes an item by index removeindex(values, 1) Same removal behavior as indexed pop()
removevalue() Removes the first matching value removevalue(values, 2) Removes the first 2
insert() Inserts an item at an index insert(values, 1, 9) Inserts 9 at index 1
len() Returns the number of elements len(values) Array length
resize() Changes the array length resize(values, 5) Resizes to five elements
reverse() Reverses the array reverse(values) Reversed order
sort() Sorts the array sort(values) Sorted values
find() Finds the index of a matching value find(values, 3) Index of first match, or -1
isvalidindex() Checks whether an index is valid isvalidindex(values, 2) 1 if valid, 0 otherwise
setdetailattrib() Writes or modifies a detail attribute setdetailattrib(0, "values", values) One array stored on the geometry
setdetailattrib(..., "append") Appends to an existing detail array setdetailattrib(0, "values", 4, "append") Adds 4 to the detail array
Array indexing Reads one element values[0] First element
Array assignment Copies an array into another variable float other[] = values; Another array variable
Array attribute assignment Stores an array on the current element f[]@values = values; Attribute class depends on the Wrangle's run-over context
set(vector) Converts vector components to an array float a[] = set(v); Three float values

Fundamentals:

An array is a variable that can hold multiple values of the same type.

<aside>

$$ {\Large \begin{array}{c} \text{Variable} \quad \rightarrow \quad \boxed{\text{One name} \rightarrow \text{One value}}\\[12pt] \text{Array} \quad \rightarrow \quad \boxed{\text{One name} \rightarrow \text{Multiple values}} \end{array} } $$

</aside>

Instead of:

float speed = 5.0;

you have:

float speeds[] = {5.0, 10.0, 15.0};

Now speeds is one variable, but it contains three float values.

You can access them by their index:

speeds
  ↓
[ 5.0, 10.0, 15.0 ]
    0     1     2

So:

speeds[0]  // 5.0
speeds[1]  // 10.0
speeds[2]  // 15.0

Attribute:

f@speeds1 = speeds[0]; // Index 0
f@speeds2 = speeds[1]; // Index 1
f@speeds3 = speeds[2]; // Index 2

image.png

<aside>

$$ \large \text{An array can contain multiple values, but all values in the array must have the same type.} $$

</aside>

// Create an array of integers
int numbers[] = {1, 2, 3};

// Create an array of floats
float values[] = {1.5, 2.5, 3.5};

// Create an array of vectors
vector positions[] = {{1, 0, 0}, {0, 1, 0}};

// Create an array of strings
string names[] = {"John", "Jane", "Bob"};

<aside> ✅

// Valid, same data type
int values[] = {1, 2, 3};

</aside>

<aside> ⛔

// Not valid, mixing data types
int values[] = {1, 2.5, "hello"};

</aside>

The Array Commandments:

<aside>

$$ {\Large\begin{array}{c}\textbf{1. One name, many values.}\\[8pt]\text{An array stores multiple values under one variable name.}\end{array}} $$

</aside>

<aside>

$$ {\Large\begin{array}{c}\textbf{2. Thou shalt use one type.}\\[8pt]\text{All values in an array must have the same type.}\end{array}} $$

</aside>

<aside>

$$ {\Large \begin{array}{c} \textbf{3. Thou shalt count from zero.}\\[8pt] \text{The first element is index }0\text{, not }1. \end{array} } $$

</aside>

<aside>

$$ {\Large \begin{array}{c} \textbf{4. Thou shalt use an index to access a value.}\\[8pt] \texttt{numbers[1]} \quad \rightarrow \quad \text{the second value} \end{array} } $$

</aside>

<aside>

$$ {\Large\begin{array}{c}\textbf{5. Thou shalt remember that an array is still a variable.}\\[8pt]\text{It's just a variable capable of holding multiple values.}\end{array}} $$

</aside>

Working with Arrays:

Declare empty Array:

// Declare an empty array of floats
float fArray[];

// Store the array as a float array attribute
f[]@fArray = fArray;

// Declare an empty array of strings
string sArray[];

// Store the array as a string array attribute
s[]@sArray = sArray;

// Declare an empty array of vectors
vector vArray[];

// Store the array as a vector array attribute
v[]@vArray = vArray;

image.png

Initialize Array with Variables

// Declare a float variable
float fval = 2.5;

// Declare a string variable
string sval = "A";

// Declare a vector variable
vector vval = {0, 1, 2};

// Initialize a float array using a variable and additional values
float fArray[] = array(fval, 2.2, 3.3);

// Initialize a string array using a variable and additional values
string sArray[] = array(sval, "B", "C", "D");

// Initialize a vector array using a variable and an additional vector
vector vArray[] = array(vval, set(3, 4, 5));

// Store the arrays as geometry attributes
f[]@fArray = fArray;
s[]@sArray = sArray;
v[]@vArray = vArray;

image.png

With:

Take this array variable and assign it to the array attribute fArray on the current element.

f[]@fArray = fArray;

So if you're running over points, each point gets its own fArray.

Accessing an Index with a Variable

// Create an array of float values
float values[] = {1, -2, 3, -4};

// Get an index from the "index" parameter
int index = chi("index");

// Array indices always start at 0
// 0 → 1, 1 → -2, 2 → 3, 3 → -4

// Use the index to access a value from the array
@P.x = values[index];

Initialize Array with Values

// Initialize an array of floats with values
float fArray[] = {1.1, 2.2, 3.3};

// Store the array as a float array attribute
f[]@fArray = fArray;

// Initialize an array of strings with values
string sArray[] = {"A", "B", "C", "D"};

// Store the array as a string array attribute
s[]@sArray = sArray;

// Initialize an array of vectors with values
vector vArray[] = {{0, 1, 2}, {3, 4, 5}};

// Store the array as a vector array attribute
v[]@vArray = vArray;

image.png

Initialize Array with setdetailattrib()

// Create individual variables
float fval = 2.5;
string sval = "A";
vector vval = {0, 1, 2};

// Create arrays using the variables
float fArray[] = array(fval, 2.2, 3.3);
string sArray[] = array(sval, "B", "C", "D");
vector vArray[] = array(vval, set(3, 4, 5));

// Store the arrays as detail attributes
setdetailattrib(0, "fvals", fArray);
setdetailattrib(0, "svals", sArray);
setdetailattrib(0, "vvals", vArray);

image.png

With:

Set the detail attribute fvals on geometry input 0 to this array.

setdetailattrib(0, "fvals", fArray);

So even if your wrangle runs over 1,000 points, there's still only one fvals array.