Expand/Shrink

vlookup

Definition: object res = vlookup(object find_item, sequence grid_data, integer source_col, target_col, object def_value=0)
Description: If the supplied item is in the source column, return the corresponding element from the target column.

find_item: An object that might exist in some grid_data[i][source_col].
grid_data: A 2D sequence that might contain find_item.
source_col: The column in which to look for find_item.
target_col: The column from which the corresponding item will be returned.
def_value: Returned when find_item is not found or the target column does not exist in that row.
pwa/p2js: Supported. As mentioned elsewhere the last 3 examples below will print 1/0 on desktop/Phix but true/false in a web browser, or you could use printf()’s %d/%t for full consistency.
Comments: If any row in the grid is actually a single atom, that row is ignored.
If the length of a row is less than source_col, the row is also ignored.
Example:
sequence grid = {{"ant", "spider", "mortein"},
                 {"bear", "seal", "gun"},
                 {"cat", "dog", "ranger"}}

?vlookup("ant", grid, 1, 2, "?") -- ==> "spider"
?vlookup("ant", grid, 1, 3, "?") -- ==> "mortein"
?vlookup("seal", grid, 2, 3, "?") -- ==> "gun"
?vlookup("seal", grid, 2, 1, "?") -- ==> "bear"
?vlookup("mouse", grid, 2, 3, "?") -- ==> "?"
Implementation: See builtins\pvlookup.e (an autoinclude) for details of the actual implementation.