table (write or) add missing row

Prev Next

Function Names

table add missing row, table write or add missing row

Description

As a first step, the table will be checked if there is a match in the columns specified in the 2nd function parameter with the data in the 3rd function parameter. The comparison technique to find the corresponding table row is the same as in the function table find(), including wildcards supported.

If no matching row has been found, then a new row will be added with the columns and values provided in the 2nd and 3rd function parameters, and, if avaiable, with the 4th and 5th function parameters, too. Header names specified in the 4th function parameter which are still missing in the table will be created in order to include the data in the table.

However, if a matching row has been found, no futher actions happen if the function table add missing row is called, or table write or add missing row with only 3 parameters is called (i.e. no further data to write). Otherwise, the further data entries will be written to the table in the identified row.

Call as: procedure or function

Restrictions

Indirect parameter passing is disabled

Parameter count

3 or 5

Parameters

No.TypeDescription
1.
input
string name of new or existing table

Table must be existing when using table append... functions. Otherwise, it does not matter.

2
input
table columns Header names and/or column numbers to compare

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

  • Any number of header names and column numbers may be specified

3
input
parameter set or string Comparison values or patterns

The nummber of elements must match with the number of headers (2nd function parameter). Specify the comparison patterns.

  • Unquoted or quoted strings as elements in parameter set: True comparison of contents without wildcards, e.g. { Name, "Street" }
  • Softquoted strings in parameter set: You can use wildcard symbols, e.g. { '* Name', 'City,Town' }
  • One quoted string specifies just one item to compare, e.g. "Last Name"
  • One softquoted string converts the contents into a parameter set and treats contents as unquoted resp. quoted strings.

Opt. 4.
input
table columns Further header names and/or column numbers 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 and column numbers may be specified
  • Nonexisting header names will be added to the table

Opt. 5
input
parameter set Further data to add or write

All data written to the table must be put into parameter set and passed in this function parameter, even if only one string value is provided. The number of elements must not exceed the number of table columns specified but may be lower. If fewer data elements are specified, then the remaining fields will not be overwritten.

Void values will not be written into the table. You can actually use void values in order to prevent updating specific fields.

Return value

TypeDescription
numeral Row number accessed

Returns row number accessed. If a row has been added, then it is the new row number at the end of the table.

Examples


  table initialize ( t,
  { { Animal, leg count, size,             intelligence },
    { Worm,       0,     very small,       low},
    { Worm,       0,     very small too,   low},
    { Fox,        4,     quite small,      medium } });


  row[] = table write or add missing row( t, { Animal, size }, { Worm, very small too }, intelligence, { negligible } );
  echo("Worm updated in row ", row[] );

  row[] = table write or add missing row( t, { Animal, size }, { Whale, huge }, intelligence, { quite high } );
  echo("Whale added in row ", row[] );

  row[] = table write or add missing row( t, Animal, { Fox } ); //
  echo("Fox is in row ", row[] );

  row[] = table add missing row( t, { Animal, leg count}, { Fox, 4 } ); // Already existing
  echo("Fox with 4 legs already exists in in row ", row[] );

  row[] = table add missing row( t, { Animal, leg count}, { Fly, 6 }, { size }, { little } ); // Already existing
  echo("Fly added in row ", row[] );

  echo;
  table list ( t );

Output

Worm updated in row 2
Whale added in row 4
Fox is in row 3
Fox with 4 legs already exists in in row 3
Fly added in row 5

    0 : Animal | leg count | size           | intelligence
    1 : Worm   | 0         | very small     | low         
    2 : Worm   | 0         | very small too | negligible  
    3 : Fox    | 4         | quite small    | medium      
    4 : Whale  |           | huge           | quite high  
    5 : Fly    | 6         | little         |             

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

See also

table add row
table find row