Lua for REXX developers
If you are a REXX developer, you might find the following tips useful.
Data types
Data in REXX is untyped; or rather, there is only one data type: string.
Lua has several types.
From Programming in Lua:
Lua is a dynamically typed language. There are no type definitions in the language; each value carries its own type.
Logical expressions
In REXX, logical expressions return 0 or 1.
From the Lua Reference Manual:
The operator
andreturns its first argument if it is false; otherwise, it returns its second argument. The operatororreturns its first argument if it is not false; otherwise, it returns its second argument.
For example:
nil or "a" --> "a"
This behavior is useful for specifying default values for function arguments:
function f(x) x = x or 1 -- Default value is 1 --- Do something with x end
Parsing
If you're looking for a Lua equivalent to the REXX PARSE statement, see:
The Simple Input Patterns (SIP) functions supplied by the Penlight extension.
The Lpeg extension.
Tables, not stem variables
Where you would use a stem variable in REXX you would likely use a table in Lua.
Modular programming
If you develop a Lua function that you want to use in more than one Lua program,
you can store the function code in a Lua program, and then use the require function
in other Lua programs to refer to that Lua program.
For example, suppose that you have developed a function named hello that you want to reuse. You store that function in a Lua program:
-- common.lua local M = {} -- Locally scoped ("private") function: -- not available outside this chunk (file) local function greeting(s) print(M.who .. " says, " .. "\"" .. s .. "\"") end M.who = "Lua" function M.hello() greeting("Hello, World!") end -- Returns a table that contains -- the hello function and the who variable return M
then use the hello function in a Lua program like this:
local cmn = require("common") cmn.hello() --> Lua says, "Hello, World!" cmn.who = "Simon" cmn.hello() --> Simon says, "Hello, World!"
The common.lua program must be stored in a path that is referred to
by the Lua package.path property.
