First off, PLEASE please please PLEASE say "Lua," not "LUA." Lua is NOT AN ACRONYM! *vents* :P
Also, as Nick said, please indent your code (I think you may need to use spaces instead of a tab), and also "block" it, meaning put comments after the end statements. Here's how it should look:function handler(document)
local audienceField = document:findField('AUDIENCE2')
if audienceField then
dataFieldvalue = document:fieldGetValue(audienceField)
result = strsplit(',', dataFieldvalue) -- Although it's a style thing, it's usually common to surround an assignment operator with whitespace
for key,value in pairs(result) do
document:addField('AUDIENCE', value)
end -- for
end -- if
end -- function handler
function strsplit(delimiter, text)
local list = { } -- Another style thing, I usually put a space in an empty array
local pos = 1
if string.find("", delimiter, 1) then
return nil
end -- if
while 1 do
local first
local last = string.find(text, delimiter, pos) -- When you're intializing a field, put it on its own line
if first then
table.insert(list, string.sub(text, pos, first - 1)) -- Whitespace surrounds binary operators
pos = last + 1 -- Whitespace surrounds binary operators
else
table.insert(list, string.sub(text, pos))
break
end -- if
end -- while
return list
end -- function strsplit
I don't mean this as an "I'm so greater than thou, BOW BEFOAR MAH MIGHT!! *BWAHAHAHAHAHA*" I'm just trying to point out the necessity and importance of legible style.
Now, to (attempt to) answer your question! :) First off, I also wonder what you mean by the snippet Nick pointed out. Did you mean string.find(text, delimiter)? (You don't really need to supply 1 as the third argument -- it's the default.) This would return nil if delimiter wasn't present in text.
Also, when there's a memory issue, it's usually because of an infinite loop where memory is allocated indefinitely. If I recall correctly, Lua's default comparison is to check if something is non-nil, not true or non-zero like in other languages. Therefore, since first is never defined as nil, the break statement will never be reached.
Would you be willing to say what, exactly, you are trying to do?
Thanks,
Terry
Edit: Also, use the [code][/code] forum tags to surround code. This no only makes it stand out, it also preserves lines (rather than wrapping them in smaller windows), and makes it monospace. |