table lookup with rules once ...

Prev Next

Function Names

table lookup with rules once, table lookup with rules once ignore case

Description

This function combines the benefits of table lookup with rules() and table lookup once(). Every entry in the look-up table is referenced only once.

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 once 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   | 1      
    3 : Cat    | small | 20    | walking  | 4         | hair        | night vision     | 1      
    4 : Fox    | small | 25    | walking  | 4         | hair        | relatively smart | 1      
    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 | 20    | walking  | 4         | hair        | *              | Cat   
    4 : small | 25    | walking  | 4         | hair        | *              | Fox   
    5 : *     | 0.01  | crawling | 0         | mucous skin | *              | Worm  

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

See also

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