lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,
With changing two 'typo', it didn't give any error for me.
I marked them with 'changed' in code.

----- Original Message -----
From: "Sebastian Rasmussen" <sebras@hotmail.com>
To: "Multiple recipients of list" <lua-l@tecgraf.puc-rio.br>
Sent: Thursday, May 23, 2002 9:30 AM
Subject: Mysterious error when referencing functions


> Hi!
>
> I've have had some problems solving the error in the code below. I can't
get
> gsub to accept a reference to a function local to the same object that the
> gsub-statement is enclosed in. self.replace returns a function naturally
> (print() proves that), but gsub fails anyway with the following error:
>
> myhost% ./test.lua
> This is my test text
> error: attempt to index global `self' (a nil value)
> stack traceback:
>    1:  method `replaceAll' at line 13 [file `./test.lua']
>    2:  function `main' at line 26 [file `./test.lua']
>    3:  main of file `./test.lua' at line 30
>
> self is nil? then how can I use self.Text in the replace() function?
> I assume that there is some really simple solution or something that I've
> missed, but I can't find the reason for the error. Can you spot the error
or
> at least point me to some literature that would explain it for me?
>
> Thanks in advance!
>
> / Sebastian Rasmussen
>
> function object(text)
>   local instance = {}
>
>   instance.Text = text
>
>   function instance:print()
>     print(self.Text)
>   end
>
>   function instance.replaceAll()

changed '.' to ':'

>     self.Text = gsub(self.Text, " *([a-zA-Z]*) *", self.replace)
>   end
>
>   function instance:replace(word)
>     reutrn "hello world"

changed 'reutrn' to 'return'

>   end
>
>   return instance
> end
>
> function main()
>   local o = object("This is my test text ")
>   o:print()
>
>   o:replaceAll()
>   o:print()
> end
>
> main()
>
>
> _________________________________________________________________
> Get your FREE download of MSN Explorer at
http://explorer.msn.com/intl.asp.
>