Indexing Parameter Sets

Prev Next

Parameter Sets - Simple Indexing

Simple indexing is used to retrieve single elements from the parameter set. Indexing begins with 0 which refers to the 1st parameter set element. The index value is specified in braces and shall not be confused with a parameter set as such. Cascaded indexing for retrieving elements from nested parameter sets is also supported.

Following rules apply to the indexing parameter sets:

  • The index must always be a numeric value.
  • Specifying just braces {} without an index will retrieve number of elements (nested elements count 1) of the parameter set.
  • Indexing begins with 0 (zero).
  • The value will be rounded in case it does not contain an integer value. Example: a[]{2.99} accesses the same member as a[3].
  • Negative indexing is supported. -1 refers to the last element. -2, -3, etc. refer to the previous elements.
  • Blanks are returned when accessing with index values which lie out of bounds. Example: Hello{5} returns a blank value.
  • Updating individual characters on the left hand side of assignments not possible. Exmaple: a[]{3} = 123;

  echo( { a,b,c,d      } {0} );                     // Access the 1st element
  a[] = { a,b,c, {d,e,f}, g };                      // Assign a parameter set to variable a[]
  echo( "Last element            : ", a[]{-1} );    // Retrieve the last element
  echo( "Next element to the left: ", a[]{-2} );    // Retrieve the 2nd last element
  echo( "Cascaded indexing       : ", a[]{3}{2} );  // retrieve 3rd element, and inside the subset the 2nd element (cascaded indexing)
  echo( "# elements in           : ", a[]{} );      // Number of elements
  echo( "# elements in nested set: ", a[]{-2}{} );  // Number of elements in subset (cascaded indexing)
  echo( "Out of bounds case      : ", a[]{99} );    // Empty is returned if index lies out of bounds
a
Last element            : g
Next element to the left: {'d','e','f'}
Cascaded indexing       : f
# elements in           : 5
# elements in nested set: 3
Out of bounds case      : {}
Try it yourself: Open LAN_Features_data_types_parameter_sets_indexing.b4p in B4P_Examples.zip. Decompress before use.

Parameter Sets - Simple Indexing in Write Accesses

Simple indexing also works with write accesses in assignments. The target variable must contain an existing parameter set. Values of other types are not allowed and will cause error messages. Any data type may be assigned. For example, a numeric element can be replaced by a string or parameter set (a subset). Using multiple indexes as allowed for read accesses is not possible (e.g. [a]{1,2} = {A,B};).

  a[] = { a,b,c, {d,e,f}, g };                      // Assign a parameter set to variable a[]
  a[]{1} = B;
  a[]{-1} = {G,H,I};
  echo( "Modified value in a[] = ", a[] );
Modified value in a[] = {'a','B','c',{'d','e','f'},{'G','H','I'}}
Try it yourself: Open LAN_Features_data_types_parameter_sets_indexing_01.b4p in B4P_Examples.zip. Decompress before use.