UDSF in Lua: Example 2
This example creates a
function called unique_chars that counts the number of unique characters in a
string and returns the result.
Code
Put the following code into the file /tmp/udsf_example2.nzl:
-- count the number of distinct letters in a string
-- USAGE: unique_chars('abc123zzzz')
function unique_chars( str )
count = 0
chars = {}
for ch in string.gmatch(str, "." ) do
if chars[ch] == null then
chars[ch] = 1
count = count + 1
end
end
return count
end
function getType()
return udf
end
function getName()
return "unique_chars"
end
function
getArgs()
return {{ "", varchar(any) }}
end
function getResult()
return "integer"
end
function getComment()
return "A Sizer Example using UDSF"
end
Compression
Compress the file /tmp/udsf_example2.nzl into a file with the name
/tmp/udsf_example2.tar.
Deployment
Deploy the UDX to a Db2® instance by issuing the following
CLPPlus
command:
SQL> IDA DEPLOYUDX SRC /tmp/udsf_example2.tar LANGUAGE lua PROJECT example2Running
Use this function in a query, for
example:
values unique_chars('abc123zzzz')This returns the following
result:1
-----------
7