Quirrel4.41.0

types.Array.remove

Method on every array
array.remove(index: number): any

Removes the element at index, shifting the rest down by one.

The binding carries no declaration string, so the VM cannot report parameter names. The names above are from this page; the types and attributes still come from the VM. The VM also cannot tell an optional parameter from a variadic tail here, so read the brackets and any trailing ... from the prose below, not from the signature.

Parameters

indexnumberwhich element to remove

Return value

The removed element, not the array.

Errors

Throws index out of range unless 0 <= index < len().

Notes

index is not adjusted for negative values the way slice and swap adjust theirs; a negative index is always out of range here, same as for insert.

Example

examples/types/array/remove.nut
let a = [1, 2, 3];
println("a.remove(0) =", a.remove(0));    // removes and returns the element, not the array
println("a =", ", ".join(a.map(@(v) v.tostring())));

try { a.remove(-1) } catch (e) { println("a.remove(-1) throws:", e); }    // negative index is not wrapped
try { a.remove(a.len()) } catch (e) { println("a.remove(a.len()) throws:", e); }
Output:
a.remove(0) = 1
a = 2, 3
a.remove(-1) throws: index out of range
a.remove(a.len()) throws: index out of range

See also

insertInserts value before the element currently at index, shifting the rest up by one.
popRemoves and returns the last element.
arrayclass index