Map data fields

Introduction

This example shows how to use the codemap.lua module to map one set of codes to another set of codes and to validate code membership in a set of codes.

If you have any questions please contact us at support@interfaceware.com.

Using the Code [top]

  • Import the Map data fields channel from the Builtin: Iguana Tools repository
  • Experiment with the code to find out how it works
  • Then add the module(s) to your Translator project
  • Copy the require statement from the channel and add it at the top of your script
    Note: This module uses require to return a table
  • Adapt the code to your own requirements
  • Use the codemap.map() function to create and return a mapping table
    • Use the returned mapping table to translate code values
  • Use codemap.set{} to create and return a mapping table to use for membership checking
    • Use the returned mapping table for checking code membership
  • Interactive scripting help is included for this module

This is the github code for the main module:

Tip: You can use any value as the key (code) in your codemap table:

How it works [top]

Basically all codemap.set{} and codemap.map{} do is create Lua tables that return a default value when there is no matching key. And that’s all you need to know – however if you want the nitty gritty then read on…

The codemap.map{} function creates a special Lua table that returns a default when the index key is not matched. The codemap.set{} function simply uses codemap.map{} to create a table where the supplied keys have values of true and the default is false.

The codemap.map{} function uses a table “m”, it then uses setmetatable() to customize the  metamethod (on table “m”) for an undefined index (__index) to return the supplied default value.

So lets see if we can paraphrase that in English: It simply means that the codemap.map{} function creates a  local table “m”. This table is like any Lua table, with one modification: when you index the table with an unknown key it calls the special __index metamethod which returns the default value supplied to the codemap.map{} function (think of it like trapping an error and returning a default value).

More information [top]

Leave A Comment?

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.