codemap.lua
Verified Featured
Added by iNTERFACEWARE
This module is used to map one set of codes to another set of codes, or to validate code membership in a set
Source Code
-- This code map library has a number of useful helper routines for doing mapping and checking codes etc. local codemap = {} -- Make a set -- MySet = codemap.set{"Fred", "Bruce", "Jim"} -- if MySet[Msg.PID[3][1][1]] then print('One of our three amigos') end function codemap.set(a) local m = {} for _,v in ipairs(a) do m[v] = true end return codemap.map(m, false) end -- Create a map the advantage of this function is that it will take a tree node without -- needing to convert it to a string. Example of usage -- SexMap = codemap.map({'M'='Male', 'm'='Male', 'F'='Female', 'f'='Female'},'defaultValue') function codemap.map(m, default) return setmetatable(m, { __index = function(m,k) if type(k) ~= 'string' then local v = rawget(m, tostring(k)) if v ~= nil then return v end end return default end}) end local codemap_map = { Title="codemap.map"; Usage="codemap.map({codemap} [, default])", SummaryLine="Create a codemap table to map one set of codes to another", Desc=[[Create a codemap table to map one set of codes to another. <p>You can add a default value to the codeset to match unknown values, if no default is used then <b>nil</b> is returned. <br><br><b>Note</b>: This function uses Lua metatables - but you do not need to understand this to use it. ]]; ParameterTable= true, Parameters= { {codemap= {Desc='Table containing the code mappings <u>table</u>.'}}, {default= {Desc='Default return if no match is found <u>string</u>.', Opt = true}}, }; Returns = { {Desc="Returns a codeset table for mapping codes <u>table</u>."}, }; Examples={ [[ -- using a default codemap value local SexCodeMap = codemap.map({ M='Male', m='Male', F='Female', f='Female' }, 'Other') -- default = 'Other' -- use the map you created to convert codes trace(SexCodeMap['M']) --> 'Male' trace(SexCodeMap['W']) --> 'Other' ]], [[ -- without a default codemap value local SexCodeMap = codemap.map({ M='Male', m='Male', F='Female', f='Female' }) -- no default -- use the map you created to convert codes trace(SexCodeMap['M']) --> 'Male' trace(SexCodeMap['W']) --> nil ]], }; SeeAlso={ { Title="codemap.lua - in our code repository", Link="http://help.interfaceware.com/code/details/codemap-lua" }, { Title="Codemap example", Link="http://help.interfaceware.com/v6/codemap-example" } } } help.set{input_function=codemap.map, help_data=codemap_map} local codemap_set = { Title="codemap.set"; Usage="codemap.set{codemap}", SummaryLine="Create a codemap table to use for membership checking", Desc=[[Create a codemap table to use for membership checking.<p> The codemap.set() function uses codemap.map() to create a codemap table with known values set to <b>true</b> and a default of <b>false</b> for unknown values. ]]; ParameterTable= true, Parameters= { {codeset= {Desc='Set of valid codes to use for membership checking <u>table</u>.'}}, }; Returns = { {Desc="Returns a mapping table for membership checking <u>table</u>."}, }; Examples={ [[ local AmigoSet = codemap.set{"Fred", "Jim", "Harry"} -- Check for set membership trace(AmigoSet['Fred']) --> true trace(AmigoSet['Mary']) --> false ]], }; SeeAlso={ { Title="codemap.lua - in our code repository", Link="http://help.interfaceware.com/code/details/codemap-lua" }, { Title="Codemap example", Link="http://help.interfaceware.com/v6/codemap-example" } } } help.set{input_function=codemap.set, help_data=codemap_set} return codemap
Description
This module is used to map one set of codes to another set of codes, or to validate code membership in a set
Attachments
Usage Details
This code simply maps one set of codes to another set of codes. You just need to create the mapping table and then use it to map your codes. An optional default value for mapping unrecognized codes can be included.
How to use codemap.lua:
- Add it to your shared modules in any Translator project
- Add
local codemap = require 'codemap'
at the top of your script - Use the
codemap.map()
function to create and return the mapping table - Use the returned mapping table to translate code values
- Use the
codemap.set()
function to create and return a table for membership checking - Use the returned table for checking code membership
Here is a basic example using the codemap module:
codemap = require 'codemap' -- The codemap module is a helpful example showing how -- easy it is to map common fields from one value to another. -- https://help.interfaceware.com/code/details/codemap-lua -- We set up the code map local SexCodeMap = codemap.map({ M='Male', m='Male', F='Female', f='Female', },'Other') local function MapCodes() -- This shows mapping functionality local V = 'M' local W = 'W' local A = SexCodeMap[V] local B = SexCodeMap[W] trace(A) trace(B) end -- Here we set up a set which we can use to test to see -- if something is a member of it. local AmigoSet = codemap.set{"Fred", "Jim", "Harry"} local function CheckSets() local Name1 = "Fred" local Name2 = "Mary" if (AmigoSet[Name1]) then trace(Name1 .. " is one of the three amigos!") end if (not AmigoSet[Name2]) then trace(Name2 .. " is not one the three amigos!") end end function main() MapCodes() CheckSets() end
Note: The code makes use of an advanced Lua feature called metatables. However, it’s not necessary to understand metatables to use the module.
More Information