Slicing Parameter Sets

Prev Next

Parameter Sets - Extracting Subsets

Instead of specifying single index values to retrieve single elements, subsets can be retrieved by listing muliple values and/or ranges. In some other programming langauges, this feature is known as slicing. The range operator '..' is available to specify a range. If no value prcedes the range operator, than 0 is assumed. If no value follows the range operator, then the last character is assumed. The range operator '..' is available to specify a range. The result will always be returned in a parameter set, even if the subset specified results in just one element, e.g. {a,b,c}{1..1} returns {b} and not b. Negative indexing is supported.

  a[] = { a,b,c, {d,e,f}, g };                          // Assign a parameter set to variable a[]
  echo( "4 elements              : ", a[]{3,1,2,1} );   // Retrieves 4 elements (2 of them repeating here)
  echo( "2nd to 2nd last         : ", a[]{1..3} );      // Use a range
  echo( "2nd to 2nd last         : ", a[]{1..-2} );     // Same output
  echo( "2nd to 2nd last         : ", a[]{-4..3} );     // Same output (-4 = 4th last or 2nd element)
  echo( "1st element             : ", a[]{0..0} );      // Return 1st element, but in a subset and not alone
  echo( "Combinations            : ", a[]{2..4,1} );    // Combination of simple values and ranges
  echo( "First three elements    : ", a[]{..2} );       // Returns first 3 elements
  echo( "Last three elements     : ", a[]{-3..} );      // Returns last 3 elements (negative indexing used here)
  echo( "Entire parameter set    : ", a[]{..} );        // Returns entire parameter set
  echo( "2nd element as subset   : ", a[]{1..1} );      // Must specify 1..1 (a range) to distinguish from __parameter set indexing__.
4 elements              : {{'d','e','f'},'b','c','b'}
2nd to 2nd last         : {'b','c',{'d','e','f'}}
2nd to 2nd last         : {'b','c',{'d','e','f'}}
2nd to 2nd last         : {'b','c',{'d','e','f'}}
1st element             : {'a'}
Combinations            : {'c',{'d','e','f'},'g','b'}
First three elements    : {'a','b','c'}
Last three elements     : {'c',{'d','e','f'},'g'}
Entire parameter set    : {'a','b','c',{'d','e','f'},'g'}
2nd element as subset   : {'b'}
Try it yourself: Open LAN_Features_Slicing_Parameter_sets.b4p in B4P_Examples.zip. Decompress before use.