Script error: not enough memory

Posted by Apatia on Wed 14 Apr 2010 03:41 PM — 6 posts, 31,678 views.

#0
HI,

I have created LUA script:

function handler(document)

local audienceField = document:findField('AUDIENCE2')

if audienceField then
dataFieldvalue = document:fieldGetValue(audienceField)
result=strsplit(',', dataFieldvalue)
for key,value in pairs(result) do
document:addField('AUDIENCE',value)
end
end
end


function strsplit(delimiter, text)
local list = {}
local pos = 1
if string.find("", delimiter, 1) then
return nil
end
while 1 do
local first, last = string.find(text, delimiter, pos)
if first then
table.insert(list, string.sub(text, pos, first-1))
pos = last+1
else
table.insert(list, string.sub(text, pos))
break
end
end
return list
end

The problem is when I run it I'm receiving two errors:

1. On win xp it is: 13/04/2010 17:02:07 [Lua - TestLuaTask] Script error: not enough memory
2. On Win 2008 64 bit: 13/04/2010 12:23:45 [Lua - TestLuaTask] Script error: table overflow

Do you have any idea why it is happening? :)
Amended on Wed 14 Apr 2010 03:42 PM by Apatia
Australia Forum Administrator #1
Quote:


if string.find("", delimiter, 1) then
  return nil
end



That will never be true, will it? You won't find the delimiter in an empty string.

Can you please indent your code? It is almost impossible to read when everything starts in column 1.
Amended on Wed 14 Apr 2010 08:17 PM by Nick Gammon
USA #2
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.
Amended on Mon 19 Apr 2010 06:33 PM by Terry
USA #3
Quote:
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.

Surely you don't actually mean that Lua won't check if something is "true" when used in an if-statement. :-)

Lua's behavior is that 'nil' and 'false' are 'false', and everything else is 'true'. This is confusing for people who expect 0 to be false. (And for Python programmers who might expect "" and empty containers to be false as well.)
USA #4
David Haley said:
Lua's behavior is that 'nil' and 'false' are 'false', and everything else is 'true'.


I was close! D: =P I knew it didn't check if it was non-zero, and that's what it seemed to be recording -- the position of the first delimiter.
Australia Forum Administrator #5

if string.find("", delimiter, 1) then
        return nil
    end -- if


Apart from the fact that it was looking for the delimiter in a fixed, empty, string.