Arrays

Prev Next

Arrays

Arrays are variables with members which are only referenced numerically using an index value. Arrays support negative indexing, meaning that -1, -2, etc. references the last, 2nd last and further array elements. A variable cannot be an array and a structure at the same time, i.e referencing an array with a member name causes an error.

Base Variable Name [ Index ]
- String value or expression - Numeral or numeric expression
as variable name as index number



Syntax for referencing arrays: Variable Name [ index ]

Following rules apply to the index:

  • The index must always be a numeric value.
  • Indexing begins with 0 (zero).
  • The value will be rounded in case it does not contain an integer value. Example: a[4.99] accesses the same member as a[5].
  • Negative index values are supported. -1 refers to the last element in the array. -2, -3, etc. refer to the next members above.
  • Exceptions are asserted when attempting to read non-existing members
  • Arrays will be extended when attempting to write using index values bigger than the current array size.



Following approaches are available to create arrays:

dim...() function family Functions such as dim(), redim(), dim protect() and redim protect() initialize arrays with a specified number of elements and initial values.
array...() Functions like array() and array protect() create arrays with initial values provided in parameter sets.
Direct referencing (Spontaneous array creation) Assigning a simple variable with an index will automatically create an array with members 0...index where the members inbetween are not initialized and return void values.



Individual member variables may be deleted using the delete() function. If a member in the beginning or middle of the arrary is deleted, then all other members move up accordingly.

  
  echo("Initialize variable with dim function:");
  dim( a[], 5, '.' );
  see( a[] );

  echo(new line, "Initialize variable with array function:");
  array( a[], { Ha, He, Hi, Ho, Hu, Ahoi } );
  see( a[] );
Initialize variable with dim function:
a[]                     [Void]                     (void,full access)
Array [   0]            .                          (softquoted string,full access)
Array [   1]            .                          (softquoted string,full access)
Array [   2]            .                          (softquoted string,full access)
Array [   3]            .                          (softquoted string,full access)
Array [   4]            .                          (softquoted string,full access)


Initialize variable with array function:
a[]                     [Void]                     (void,full access)
Array [   0]            Ha                         (softquoted string,full access)
Array [   1]            He                         (softquoted string,full access)
Array [   2]            Hi                         (softquoted string,full access)
Array [   3]            Ho                         (softquoted string,full access)
Array [   4]            Hu                         (softquoted string,full access)
Array [   5]            Ahoi                       (softquoted string,full access)

Try it yourself: Open LAN_Features_arrays.b4p in B4P_Examples.zip. Decompress before use.

Nesting

Nesting is supported: Every array element may be a simple variable, a further array of any size or a structure. A (m x n) multi-dimensional array is a nested array with m members and each member containing n sub-members. Since Beyond4P variable structure is a tree with values possible in both root, intermediate and end nodes, the base variables as well as member variables of those containing sub-members are preserved.
Nested arrays:

Base Variable Name [ Index , Index ]
- String value or expression - Numeral or numeric expression - Numeral or numeric expression
as variable name as index number as index number

  • Nesting structures in arrays and vice versa is also allowed.
  • For deeper nesting, specify addtional index numbers and/or member names separated with commas.


  echo("Initialize a 2-dimensional array:");
  dim( a[], {2,3}, '0' );
  see( a[] );

  echo(new line, "Initialize two arrays (2nd one is nested) implicitly:");
  b[2] = true;
  b[1,2] = 123;
  see( b[] );
Initialize a 2-dimensional array:
a[]                     [Void]                     (void,full access)
Array [   0]            0                          (softquoted string,full access)
  Array [   0]          0                          (softquoted string,full access)
  Array [   1]          0                          (softquoted string,full access)
  Array [   2]          0                          (softquoted string,full access)
Array [   1]            0                          (softquoted string,full access)
  Array [   0]          0                          (softquoted string,full access)
  Array [   1]          0                          (softquoted string,full access)
  Array [   2]          0                          (softquoted string,full access)


Initialize two arrays (2nd one is nested) implicitly:
b[]                     [Void]                     (void,full access)
Array [   0]            [Void]                     (void,full access)
Array [   1]            [Void]                     (void,full access)
  Array [   0]          [Void]                     (void,full access)
  Array [   1]          [Void]                     (void,full access)
  Array [   2]          123  "123"                 (numeral,full access)
Array [   2]            true                       (boolean,full access)

Try it yourself: Open LAN_Features_arrays_01.b4p in B4P_Examples.zip. Decompress before use.