Posted 07 November 2012 - 05:29 PM
Help me get my head around something. I found a string splitting function on the lua users' wiki that is defined as an instance method on strings (forgive me if I'm using non-Lua terms here, still picking up the language), i.e., function string:split(). After having issues using it in a turtle program, I extracted it to a standalone file along with a simple test:
I can run this in the standalone lua interpreter just fine, but it fails in game with 'Attempt to call nil' at the l = x:split('%s+') line. If I change that line to non-method syntax, i.e., l = string.split(x, '%s+') it works in both places. I note that while I can make it work by changing the calling sytax, I didn't have to change the definition; so declaring the the function in method notation is fine (string:split) but calling it that way isn't? Is this just a limitation of CC's lua implementation? Or am I missing a bigger issue here?
TIA,
Jason
Spoiler
function string:split( inSplitPattern, outResults )
if not outResults then
outResults = { }
end
local theStart = 1
local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
while theSplitStart do
table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
theStart = theSplitEnd + 1
theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
end
table.insert( outResults, string.sub( self, theStart ) )
return outResults
end
x = 'foo bar baz'
l = x:split('%s+')
print(l[1])
print(l[2])
print(l[3])
I can run this in the standalone lua interpreter just fine, but it fails in game with 'Attempt to call nil' at the l = x:split('%s+') line. If I change that line to non-method syntax, i.e., l = string.split(x, '%s+') it works in both places. I note that while I can make it work by changing the calling sytax, I didn't have to change the definition; so declaring the the function in method notation is fine (string:split) but calling it that way isn't? Is this just a limitation of CC's lua implementation? Or am I missing a bigger issue here?
TIA,
Jason