table add/insert calculated columns

Prev Next

Function Names

table add calculated columns, table insert calculated columns

Description

This function inserts one or more columns into the table where the intial values are calculated row-by-row with the expressions provided.

Call as: procedure or function

Restrictions

Indirect parameter passing is disabled

Parameter count

min. 3

Parameters

No.TypeDescription
1.
input
string Name of existing table

opt. 2
input
table column Destination column

Applicable for table insert calculated columns only. The other function table add calculated columns adds all columns at the right end of the table. See table columns as function parameters for general ruling for this parameter.
Specific rules apply for this function:

  • Specify only one header name and column number

The new column will be inserted to the left. To insert them to the right, using shifted Table Column Specifications is a possible option, or use column numbers. Negative indexing is supported, where -1 adds the new column after (and not before) the last column.

2 / 3
input
table columns Header names columns to add

See table columns as function parameters for general ruling for this parameter.
Specific rules apply for this function:

  • Any number of header names may be specified. Column numbers are not allowed here.

3, 4.. / 4, 5,...
code
expression
:string
expressionfor initial values

The expressions will be calculated for every row so the initial values may differ from row to row. Ideally, provide the same number of expressions as the number of columns specified. If more expressions are provided than the number of columns specified, then the excess expressions will not be calculated. If fewer expressions are provided than the number of columns specified, then the uncovered columns will be initialzed with blanks.

Return value

TypeDescription
numeral Number of headers inserted

-1 is returned if no header row has been found

Exceptions

Table not found

Examples

       table initialize( t,
               { { First Name, Last Name },
                 { Abel,       Adams     },
                 { Betty,      Beavers   },
                 { Charlie,    Collins   },
                 { Daniela,    Douglas   } } );

       table copy table (t, u );
       table add calculated columns( u, { Full Name, Abbreviation, Character Count },
                [First Name] + ' ' + [Last Name], left([First Name], 1), [Full Name]{} );

       table list( u );

       table insert calculated columns( t, 0, { Full Name, Abbreviation, Character Count },
                [First Name] + ' ' + [Last Name], left([First Name], 1), [Full Name]{} );

       table list( t );

Output

    0 : First Name | Last Name | Full Name       | Abbreviation | Character Count
    1 : Abel       | Adams     | Abel Adams      | A            | 10             
    2 : Betty      | Beavers   | Betty Beavers   | B            | 13             
    3 : Charlie    | Collins   | Charlie Collins | C            | 15             
    4 : Daniela    | Douglas   | Daniela Douglas | D            | 15             

    0 : Full Name       | Abbreviation | Character Count | First Name | Last Name
    1 : Abel Adams      | A            | 10              | Abel       | Adams    
    2 : Betty Beavers   | B            | 13              | Betty      | Beavers  
    3 : Charlie Collins | C            | 15              | Charlie    | Collins  
    4 : Daniela Douglas | D            | 15              | Daniela    | Douglas  

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

See also

table delete columns
table copy columns