Syntax
Reads the size of an array, resizes it, inserts, removes, finds and sorts cells, through members written after a dot.
<array>.length
<array>.length = <last>
<array>.insert(<value>[, <index>])
<array>.remove([<index>])
<array>.find(<value>)
<array>.sort()
UNDIM <array>[]Also written : .length, .remove, .find, .sort
Parts
| <array> | An array from DIM or AS <type>[], also an array field of a record (bag.names). |
|---|---|
| <last> | The new last index. Kept cells keep their values; new cells get the array's default. -1 empties the array. |
| <value> | For .insert, the value to add. For .find, the value to look for, compared with strict equality: "2" does not find the number 2. |
| <index> | Optional position. .insert puts the value there and shifts the rest; without it, at the end. .remove deletes that cell; without it, the last one. |
Good to know
.lengthis the last index, not the count: the number of cells isa.length + 1, and an empty array gives -1. On a grid,g.lengthis the last index of the first dimension,g[0].lengthof the second..findreturns the index of the first equal cell, or -1. It scans the whole array, sorted or not..insert,.removeand.length = nare statements on their own line: they return nothing, andx = a.insert(5)stops the program..sortsorts in place, smallest first. Strings compare by character code: every capital letter comes before every small one. An array of records is sorted on its first field..insertdoes not convert the value to the array's type: onDIM a(0) AS integer,a.insert(2.7)stores 2.7. Assign the cell afterwards (a(1) = 2.7stores 2) to get the conversion..length = non a grid adds plain 0 cells, not rows: writes into the new rows are lost. Redeclare the grid withDIMinstead.UNDIM a()compiles into a program that does not start. WriteUNDIM a[].
Example
DIM a(2) = [30, 10, 20]a.insert(5)a.insert(99, 0)Console(Str(a.length)) ' 4a.remove()a.remove(0)a.sort()Console(Str(a(0)) + " " + Str(a(1)) + " " + Str(a(2))) ' 10 20 30Console(Str(a.find(20)) + " " + Str(a.find(42))) ' 1 -1a.length = 4Console(Str(a.length) + " " + Str(a(4))) ' 4 0UNDIM a[]Console(Str(a.length)) ' -1In the manual : Arrays, lists and maps