table lookup with rules ...

Prev Next

Function Names

table lookup with rules, table lookup with rules ignore case

Description

This function is a kind of opposite to table lookup smart(). The B4P comparison expressions and expressions are specified in the target table and the lookup table contains the actual values to be checked and retrieved. Examples to be specified in the input columns in the target table (Syntax is same as in B4P expressions):

Building Equals to "Building"
=Building Equals to "Building"
<>Building Must not equal to "Building"
B* Example of wildcard symbols: Begins with B
1..10,20 True if between 1 and 10, or equal to 20.
'3..5' Equals to "3..5" (Text string)
>10 Greater than 10
Dog,*at,Fly Commas allow for flexible seletion OK for Dog, Cat, Rat, Bat, Fly, Gnat, ...
Dog,?at,Fly Commas allow for flexible seletion OK for Dog, Cat, Rat, Bat, Fly, but not Gnat (? is placeholder for 1 character)

Don't care

?*

Must contain at least 1 characters, contents don't care.

Examples: You have a product portfolio listed in the lookup table and your target table contains some specifications, e.g. performance ranges, matching features, etc. In this case, the lookup table containing the values is searched from to to bottom and compared with the comparison expressions provided in the target table. Once a match is identified, then the results are copied from the lookup table to the target table.

Beginning with release 8.03, B4P expressions returning Boolean values can be specified as alternatively. The contents are recognized as B4P expressions if they start with a colon (:). In the expression, the context for partial table references is provided, referring to the current row and column in the target table being compared. Example: :even([.]) considers value as found if the value in the target table contains an even number. In these expressions, other columns in the target table can be referenced by specifying the corresponding column name or number.

Examples to be specified in the input columns in the target table (Syntax is same as in B4P expressions):

:[.]=Building Equals to "Building"
:[.]{0}=B Begins with B (comparing the 1st character in the string)
:[.]{}=4 Contents must consist of 4 characters
:[.]=[Town] true if the contents in the current column equals to the contents in column named "Town" in the target table
:odd([.]) true if contents contain odd numbers



Note: The 'ignore case' option in the function name does not influence execution of B4P expressions explained above. In order to ignore cases, use the '+' modifier in the comparison, e.g. [.]=+'hello'.

The search time ratio versus table size n is O ( n) because every search is a top-down search.

Call as: procedure or function

Restrictions

Indirect parameter passing is disabled

Parameter count

6 - 8 (For parameter description and return values: see table lookup() )

Examples


  table initialize( animals,
      { { Name,      Size,     Speed,     Movement,  Leg count,  Surface,    Special Skills   },
        { Parrot,    small,    50,        flying,    2,          feathers,   immitates voices },
        { Dog,       small,    30,        walking,   4,          hair,       sense of smell   },
        { Cat,       small,    20,        walking,   4,          hair,       night vision     },
        { Fox,       small,    25,        walking,   4,          hair,       relatively smart },
        { Owl,       small,    80,        flying,    2,          feathers,   night vision     },
        { Worm,      tiny,     0.01,      crawling,  0,          mucous skin,unclear          },
        { Whale,     huge,     30,        swimming,  0,          skin,       relatively smart } } );

  headers[] = [animals:..,0]-{Name};


  table initialize( target,
          { { Size,     Speed,     Movement,  Leg count,  Surface,    Special Skills, Animal   },
            { '*',      ':mod([.],40)=0','*', '*',        '*',        '*'              }, // Speed divisible by 40 -> Owl
            { small,    '20..30',  '*',       '*',        Hair,       '*'              }, // Dog
            { small,    '20..30',  '*',       '*',        Hair,       '*'              }, // Dog
            { small,    '20..30',  '*',       '*',        Hair,       '*'              }, // Dog
            { '*',      '<1',      '*',       '*',        '*',        '*'              } } );

  echo("Target table before lookup:");
  table list( target );


  echo("You can actually overwrite columns containing rules with the answers looked up, e.g. Leg count, surface, and speed");
  count[] = table lookup with rules ignore case ( target,  headers[], { Animal, Leg count, Surface, Speed, Movement },
                                                  animals, headers[], { Name,   Leg count, Surface, Speed, Movement }, true, Counter );

  echo("List of animals with counted lookups:");
  table list( animals );

  echo("Target table after lookup (", count[], " items found):");
  table list( target );

Output

Target table before lookup:
    0 : Size  | Speed          | Movement | Leg count | Surface | Special Skills | Animal
    1 : *     | :mod([.],40)=0 | *        | *         | *       | *              |       
    2 : small | 20..30         | *        | *         | Hair    | *              |       
    3 : small | 20..30         | *        | *         | Hair    | *              |       
    4 : small | 20..30         | *        | *         | Hair    | *              |       
    5 : *     | <1             | *        | *         | *       | *              |       

You can actually overwrite columns containing rules with the answers looked up, e.g. Leg count, surface, and speed
List of animals with counted lookups:
    0 : Name   | Size  | Speed | Movement | Leg count | Surface     | Special Skills   | Counter
    1 : Parrot | small | 50    | flying   | 2         | feathers    | immitates voices |        
    2 : Dog    | small | 30    | walking  | 4         | hair        | sense of smell   | 3      
    3 : Cat    | small | 20    | walking  | 4         | hair        | night vision     |        
    4 : Fox    | small | 25    | walking  | 4         | hair        | relatively smart |        
    5 : Owl    | small | 80    | flying   | 2         | feathers    | night vision     | 1      
    6 : Worm   | tiny  | 0.01  | crawling | 0         | mucous skin | unclear          | 1      
    7 : Whale  | huge  | 30    | swimming | 0         | skin        | relatively smart |        

Target table after lookup (5 items found):
    0 : Size  | Speed | Movement | Leg count | Surface     | Special Skills | Animal
    1 : *     | 80    | flying   | 2         | feathers    | *              | Owl   
    2 : small | 30    | walking  | 4         | hair        | *              | Dog   
    3 : small | 30    | walking  | 4         | hair        | *              | Dog   
    4 : small | 30    | walking  | 4         | hair        | *              | Dog   
    5 : *     | 0.01  | crawling | 0         | mucous skin | *              | Worm  

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

See also

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