githubEdit

Arrays

OSL provides a rich set of methods for working with arrays. Arrays in OSL are 1-indexed - the first element is at index 1.

Creating Arrays

// Array literal
arr = [1, 2, 3]

// Range operator
arr = 1 to 5  // [1, 2, 3, 4, 5]
arr = -2 to 2 // [-2, -1, 0, 1, 2]

// Fill
arr = (1 to 3).fill("hi") // ["hi", "hi", "hi"]

Concatenation

// Concatenate arrays with ++
arr = [1, 2] ++ [3, 4]  // [1, 2, 3, 4]

// Prepend value to array
arr = "hello" + ["world"]  // ["hello", "world"]

// Append value to array
arr = ["hello"] + "world"  // ["hello", "world"]

// Concat method
arr = [1, 2, 3].concat([4, 5, 6])  // [1, 2, 3, 4, 5, 6]

Accessing Elements

Searching

Modifying

Transforming

Aggregating

Random

Type Prototypes

You can add custom methods to arrays using type prototypes:

Notes

  • Arrays use 1-based indexing (first element is at index 1)

  • Index 0 returns null (out of bounds)

  • Negative indices work from the end (-1 is last element)

  • Methods that modify in-place return null - use void to discard the return value

Last updated