Expressions to Select Columns

Prev Next

Introduction

Expressions to select rows are a variant of code pieces containing expressions, but provide additional flexibility. These expressions are typically used in functions with names containing ... selected columns ... where expressions are applied to chosses specifc columns to include in the data processing.

Like expressions specified as code pieces, these expressions can be coded directly or provided as strings with preceding colon (:) symbols.

The expressions may return values of following types:

boolean If it is an expression returning boolean values, then this expression will be calculated for every column, starting from the left with column 0. Typically, processing will be done on the corresponding columns if the outcome is true.
string if the expression returns a string, then a header name is assumed. Only existing header names will be accepted.
numeral If the expression returns a numeral, then one single column number is meant. Negative indexing is allowed, e.g. -1 refers to the last column with respect to the header row.
parameter set If the expression returns a parameter set, the the column numbers inside the parameter sets will be used. Negative indexing is allowed as described above. The parameter set may also contain existing header names. No columns will be processed if an empty set is provided.


Note: Execution of selected columns will always be from left to right. Every column will be executed at most once, even if specified multiple times. Example: {5,2,1,2,4} will execute columns 1, 2, 4 and finally 5 in this order only once.

  table initialize ( animals,
  { { Animal,    Snake, Bird, Dog, Fly, Lobster, Decapod, Woodlice, Centipede },
    { Leg count, 0,     2,    4,   6,   8,       10,      14,       100       },
      Result 1,
      Result 2,
      Result 3 } );

  counter[] = 0; // Process columns in specified order. (9..8..5 is from right to left)
  table process selected columns  ( animals, 2, {1..3, 10, 5..7}, [.] = ++counter[], [.] = '-' );

  counter[] = 0; // Process columns in specified order
  table process selected columns  ( animals, 3, {Decapod, Bird, Fly, Dog, Dog, Dog}, [.] = ++counter[], [.] = '-' );

  counter[] = 0; // From left to right: All animals beginning with A..D, excl. 1st column
  table process selected columns  ( animals, 4, col()>0 & [.,0] < 'E', [.] = ++counter[], [.] = '-' );

  table list( animals );
    0 : Animal    | Snake | Bird | Dog | Fly | Lobster | Decapod | Woodlice | Centipede |   |  
    1 : Leg count | 0     | 2    | 4   | 6   | 8       | 10      | 14       | 100       |   |  
    2 : -         | 1     | 2    | 3   | -   | 4       | 5       | 6        | -         | - | 7
    3 : -         | -     | 1    | 2   | 3   | -       | 4       | -        | -         |   |  
    4 : -         | -     | 1    | 2   | -   | -       | 3       | -        | 4         |   |  

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