Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ Script error: not enough memory

Script error: not enough memory

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Apatia   (1 post)  Bio
Date Wed 14 Apr 2010 03:41 PM (UTC)

Amended on Wed 14 Apr 2010 03:42 PM (UTC) by Apatia

Message
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? :)
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #1 on Wed 14 Apr 2010 08:16 PM (UTC)

Amended on Wed 14 Apr 2010 08:17 PM (UTC) by Nick Gammon

Message
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Terry   USA  (87 posts)  Bio
Date Reply #2 on Mon 19 Apr 2010 06:27 PM (UTC)

Amended on Mon 19 Apr 2010 06:33 PM (UTC) by Terry

Message
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.
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #3 on Mon 19 Apr 2010 08:12 PM (UTC)
Message
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.)

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Terry   USA  (87 posts)  Bio
Date Reply #4 on Mon 19 Apr 2010 10:17 PM (UTC)
Message
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.
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #5 on Tue 20 Apr 2010 12:02 AM (UTC)
Message

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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


24,926 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.