Modifying An Array

Basic Operations

Arrays in OSL provide several methods for modification and access.

arr = [1, 2, 3, 4]

// Accessing elements (1-indexed)
first = arr[1]    // 1
second = arr[2]   // 2

// Modifying elements
arr[1] = 10      // [10, 2, 3, 4]

// Getting length
length = arr.len  // Number of elements

Adding and Removing Elements

// Adding elements
arr.append(5)     // Adds to end
arr.prepend(0)    // Adds to beginning

// Removing elements
last = arr.pop()    // Removes and returns last element
first = arr.shift() // Removes and returns first element

Array Methods

Array Merging

Array Destructuring

Important Notes

  • Arrays are 1-indexed in OSL

  • Most operations create new copies

  • .trim() is used for array slicing

  • Destructuring can skip elements using empty slots

  • The ++ operator combines arrays

  • Methods like sort() and map() return new arrays

Last updated

Was this helpful?