LuaFy – a free Lua scripting plug-in for FileMaker Pro®

LuaFy Examples

Sorting a Value List
Simple Pattern Matching/Substitution
Generating a Sophisticated Alphanumeric Sort Key
 
LuaFy_Examples.fp7 (.zip)  47 kB
LuaFy_CallFunction and Resume Example.fp7 (.zip)  302 kB
 
Download and Feature List
Purchase a License to LuaFy Plus
Functions Provided by LuaFy
Lua Libraries Provided by LuaFy
Lua Integration with FileMaker
Usage Examples
What is Lua?
Origin Story
Version History & Old Versions

Sorting a Value List

Example:
LuaFy_RunScript(
"local theList=...; local t=luafy.totable(theList);table.sort(t);return luafy.tolist(t)";
"apple¶zookeeper¶bacon"
)

Result:
apple
bacon
zookeeper

Benchmarking with FileMaker Pro Advanced 11 on an Intel Macintosh shows that sorting with LuaFy in this way is about 250 times faster than using the custom function SortList() when sorting 1000 items (1 sec. versus 0.004 sec.). Lua is over 1000 times faster when sorting 10,000 items. Sorting 100,000 entries in a FileMaker value list is no problem for Lua (~ 0.5 sec on a 2009 Quad-core Mac Pro).

Note that when comparing the results of Lua's sorting versus FileMaker sorting, remember that Lua's default sort order is case-sensitive and in ASCII order. You can make Lua's table.sort() be case-insensitive by providing the optional second argument that makes the comparision convert both arguments to lower case before comparing them, e.g.
table.sort(myArray, function(t1,t2) return (t1:lower() < t2:lower() ) end )

Pattern Matching/Substitution

Example: LuaFy_CallFunction("string.match";"We have 42 products in stock."; "%d+ %a+")
Result: 42 products

Example:
LuaFy_RunScript("
local r=''
local t=...
for theMatch in string.gmatch(t , '%d+ %a+') do
r = r .. theMatch .. '\r'
end
return r
";
"We have 3 blue, 2 green, and 5 red in stock.")

Result:
3 blue
2 green
5 red

Example: LuaFy_CallFunction("string.gsub"; "This is an example sentence."; "(exam%a+)"; "excellent")
Result: This is an excellent sentence.

Generating a Sophisticated Alphanumeric Sort Key

Say that you have a records with a identifier that has a mixture of letters and numeric digits. You want to sort based on the identifier but FileMaker Pro's alphabetic sorting does not recognize the numeric sections of your identifier in the way you would like. If you could generate a sort key that padded all of the numbers with zeros so that they would sort alphabetically, then you could simply sort using that new sort key. If your digits appear in a variety of places in the string, this could present a significant challenge for a FileMaker Pro calculation.

These identifiers are in alphabetical order but not the way a human may prefer to see them.
ID1S20
ID1S4
ID100S1
ID2S4
ID3S2
P1
P15
P2

If you prefer to see them in this order... ID1S4
ID1S20
ID2S4
ID3S2
ID100S1
P1
P2
P15
Then the sort key needs to be something like the following.

Record #IdentifierDesired Sort Key
1ID1S20 ID00000001S00000020
2ID1S4 ID00000001S00000004
3ID100S1 ID00000100S00000001
4ID2S4 ID00000002S00000004
5ID3S2 ID00000003S00000002
5P1 P00000001
5P15 P00000015
5P2 P00000002

Lua's string.gsub() can handle this if we use a function instead of a simple pattern as the replacement. If your identifier is in the field named "SKU", you can create a calculated field "SortKey" which can be defined with this short calculation:

LuaFy_RunScript("
local sortKey = ...
if (sortKey == nil) or (sortKey == '') then return '' end
sortKey = sortKey:gsub('(%d+)', function(n) return string.format('%08d',n) end)
return sortKey
";
SKU
)

Any sequence of digits in the input will be expanded to 8 digits by padding on the left with zeros.


LuaFy is a product of Cosmic Consulting
Tom Hays <trhays@luafy.com>