Array members

Statement

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

  • .length is the last index, not the count: the number of cells is a.length + 1, and an empty array gives -1. On a grid, g.length is the last index of the first dimension, g[0].length of the second.
  • .find returns the index of the first equal cell, or -1. It scans the whole array, sorted or not.
  • .insert, .remove and .length = n are statements on their own line: they return nothing, and x = a.insert(5) stops the program.
  • .sort sorts 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.
  • .insert does not convert the value to the array's type: on DIM a(0) AS integer, a.insert(2.7) stores 2.7. Assign the cell afterwards (a(1) = 2.7 stores 2) to get the conversion.
  • .length = n on a grid adds plain 0 cells, not rows: writes into the new rows are lost. Redeclare the grid with DIM instead.
  • UNDIM a() compiles into a program that does not start. Write UNDIM 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))   ' -1
In the manual : Arrays, lists and maps

See also