View this PageEdit this PageUploads to this PageHistory of this PageTop of the SwikiRecent ChangesSearch the SwikiHelp Guide

How to access an element in an Array

the usual way in C to access elements in arrays
array[i] is not used in SC. SC Objects respond
to the .at(i) method - [1,2,3].at(1) returns 2.
this is not documented, as .at is a method of Object already.

put(i, item) is used to change a value in the array.

if the index is larger than the size of the array - 1 it returns nil.
to avoid this one can use clipAt(i) and clipPut(i) so the last or the first item
in the array is taken when the index is too large or too small.

other indexing methods can be explored easily:

//atModify
a = [1,2,3];
a.atModify(1, { arg item; a.put(1, item * 8) });
a.postln;

//atInc
a = [1,2,3];
a.atInc(1);
a.postln;
a.atInc(0, 889);
a.postln;

//atDec
a = [1,2,3];
a.atDec(1, 10);
a.postln;
a.atDec(1);
a.postln;

//removeAt
a = [1, 2, 3, 4, 5];
x = a.removeAt(0);
[x, a].postln;

//takeAt
a = [1, 2, 3, 4, 5];
x = a.takeAt(0);
[x, a].postln;

Link to this Page