table expand ...

Prev Next

Function Names

table expand, table expand ignore case

Description

These functions work very similarly like table integrate(). Instead of stopping at the first match in the lookup table, this function family looks up all entries in the lookup table. The search procedure through the lookup table will always be top-down. If only one match has been identified, then the behavior is the same as in table integrate(). In case 2 or more matching enties are found in the lookup table, then the following happens:

  • In the target table, additional rows will be inserted below the current row being procesed. The rows further below will shift downward accordingly.
  • All contents in the original row will be copied into these new rows.
  • Then the looked up information will be integrated into the corresponding target rows, using the same principle as in the function table integrate() using the operation defined by the table integration operation identifiers.
  • Some operation identifiers make use of special rules for dealing with blank contents in the lookup table.
  • In case there is no match for a partiuclar row in the target table, then the row stays unchanged (meaning that these rows will not be removed).

Function 'table expand'

Additional features:

  • An expression to select rows (8th function parameter) can be specified to limit the lookup process to selected rows.
  • An additional column (9th function parameter) can be specified in the lookup table to count the number of matches.

Call as: procedure or function

Restrictions

Indirect parameter passing is disabled

Parameter count

6 - 9 (For parameter description and return values: see table integrate() )

Examples


  table initialize( nute facts, // Nutritional facts, source: ndb.nal.usda.gov
  {  {  Food, Ingredient, Portion  },
     {  potato chips,  fat,            0.34  },
     {  potato chips,  carbohydrates,  0.50  },
     {  potato chips,  sodium,         0.08  },
     {  potato chips,  proteins,       0.07  },
     {  potato chips,  others,           ''  }, // Remaining weight subtracted from above (--> operation identifer 'mul')
     {  broccoli,      water,          0.90  },
     {  broccoli,      proteins,       0.03  },
     {  broccoli,      carbohydrates,  0.06  },
     {  broccoli,      others,           ''  }, // Remaining weight subtracted from above (--> operation identifer 'mul')
     {  spring water,  water,          1.00  } } );

  table initialize( target, { { Name, Favorite Food, Weight },
     { Rafael, Broccoli,    200 }, { Steve, Parmesan cheese, 100 }, { Stephanie, Parmesan cheese, 200 },
     { Jane,   Potato chips,100 }, { Nick, Potato chips, 50 },      { Fred,  Spring water,   1000 } } );


  echo("Target table before looking up with expanding contents:");
  table list ( target );

  count[] = table expand ignore case( target, Favorite Food, { Ingredient, Weight }, nute facts, Food,
                { Ingredient, Portion }, { overwrite, mul}, true, Counter );

  echo("Lookup table:");
  table list ( nute facts );

  // Note that food portions are multiplied with the weight of the food.

  echo("After expanding (listing ingredients and calculating their weights):");
  echo("Note: Nothing found for parmesan cheese.");
  echo("Target table after lookup (", count[], " items found):");
  table list ( target );

Output

Target table before looking up with expanding contents:
    0 : Name      | Favorite Food   | Weight
    1 : Rafael    | Broccoli        | 200   
    2 : Steve     | Parmesan cheese | 100   
    3 : Stephanie | Parmesan cheese | 200   
    4 : Jane      | Potato chips    | 100   
    5 : Nick      | Potato chips    | 50    
    6 : Fred      | Spring water    | 1000  

Lookup table:
    0 : Food         | Ingredient    | Portion | Counter
    1 : potato chips | fat           | 0.34    | 2      
    2 : potato chips | carbohydrates | 0.50    | 2      
    3 : potato chips | sodium        | 0.08    | 2      
    4 : potato chips | proteins      | 0.07    | 2      
    5 : potato chips | others        |         | 2      
    6 : broccoli     | water         | 0.90    | 1      
    7 : broccoli     | proteins      | 0.03    | 1      
    8 : broccoli     | carbohydrates | 0.06    | 1      
    9 : broccoli     | others        |         | 1      
   10 : spring water | water         | 1.00    | 1      

After expanding (listing ingredients and calculating their weights):
Note: Nothing found for parmesan cheese.
Target table after lookup (15 items found):
    0 : Name      | Favorite Food   | Weight | Ingredient   
    1 : Rafael    | Broccoli        | 180    | water        
    2 : Rafael    | Broccoli        | 6      | proteins     
    3 : Rafael    | Broccoli        | 12     | carbohydrates
    4 : Rafael    | Broccoli        | 2      | others       
    5 : Steve     | Parmesan cheese | 100    |              
    6 : Stephanie | Parmesan cheese | 200    |              
    7 : Jane      | Potato chips    | 34     | fat          
    8 : Jane      | Potato chips    | 50     | carbohydrates
    9 : Jane      | Potato chips    | 8      | sodium       
   10 : Jane      | Potato chips    | 7      | proteins     
   11 : Jane      | Potato chips    | 1      | others       
   12 : Nick      | Potato chips    | 17     | fat          
   13 : Nick      | Potato chips    | 25     | carbohydrates
   14 : Nick      | Potato chips    | 4      | sodium       
   15 : Nick      | Potato chips    | 3.5    | proteins     
   16 : Nick      | Potato chips    | 0.5    | others       
   17 : Fred      | Spring water    | 1000   | water        

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

See also

table expand fast
table expand smart
table expand with rules
table expand with rules once
table lookup
table integrate
table digest