“错误:尝试在string.split函数中索引本地'self'(零值)”

时间:2011-09-25 21:01:22

标签: lua split corona

快速的事实,我从最底层的http://lua-users.org/wiki/SplitJoin获得了这个功能,并试图在Corona SDK中使用它,尽管我怀疑这很重要。

function string:split(sSeparator, nMax, bRegexp)
    assert(sSeparator ~= '')
    assert(nMax == nil or nMax >= 1)

    local aRecord = {}

    if self:len() > 0 then
        local bPlain = not bRegexp
        nMax = nMax or -1

        local nField=1 nStart=1
        local nFirst,nLast = self:find(sSeparator, nStart, bPlain)
        while nFirst and nMax ~= 0 do
            aRecord[nField] = self:sub(nStart, nFirst-1)
            nField = nField+1
            nStart = nLast+1
            nFirst,nLast = self:find(sSeparator, nStart, bPlain)
            nMax = nMax-1
        end
        aRecord[nField] = self:sub(nStart)
    end

    return aRecord
end

输入:“1316982303搜索服务器”

msglist = string.split(msg, ' ')

给我标题中的错误。有任何想法吗?我很确定这只是功能已经过时了。

编辑:更多代码 这是main.lua文件中的更多内容:

multiplayer = pubnub.new({
    publish_key   = "demo",             
    subscribe_key = "demo",             
    secret_key    = nil,                
    ssl           = nil,                -- ENABLE SSL?
    origin        = "pubsub.pubnub.com" -- PUBNUB CLOUD ORIGIN
})


multiplayer:subscribe({
    channel  = "MBPocketChange",
    callback = function(msg)
        -- MESSAGE RECEIVED!!!
        print (msg)
        msglist = string.split(msg, ' ')
        local recipient = msglist[0]  --Get the value
        table.remove(msglist, 0)     --Remove the value from the table.
        local cmdarg = msglist[0]
        table.remove(msglist, 0)
        arglist = string.split(cmdarg, ',')
        local command = arglist[0]
        table.remove(arglist, 0)
        argCount = 1
        while #arglist > 0 do
            argname = "arg" .. argCount
            _G[argname] = arglist[0]
            table.remove(arglist, 0)
            argCount = argCount + 1
        end

Server.py: 这是多人服务器,它向客户端发送必要的信息。

import sys
import tornado
import os
from Pubnub import Pubnub

## Initiat Class
pubnub = Pubnub( 'demo', 'demo', None, False )

## Subscribe Example
def receive(message) :
    test = str(message)
    msglist = test.split()
    recipient = msglist.pop(0)
    msg = msglist.pop(0)
    id = msglist.pop(0)
    if id != "server":
        print id
        print msg
        commandHandler(msg,id)
        return True

def commandHandler(cmd,id):
    global needOp
    needOp = False
    global matchListing
    if server is True:
        cmdArgList = cmd.split(',')
        cmd = cmdArgList.pop(0)
        while len(cmdArgList) > 0:
            argument = 1
            locals()["arg" + str(argument)] = cmdArgList.pop(0)
            argument += 1
        if cmd == "Seeking":
            if needOp != False and needOp != id:
                needOp = str(needOp)
                id = str(id)
                pubnub.publish({
                    'channel' : 'MBPocketChange',
                    #Message order is, and should remain:
                    #----------Recipient, Command,Arguments, Sender
                    'message' : needOp + " FoundOp," + id + " server"
                })
                print ("Attempting to match " + id + " with " + needOp + ".")
                needOp = False
                matchListing[needOp] = id
            else:
                needOp = id
                pubnub.publish({
                    'channel' : 'MBPocketChange',
                    #Message order is, and should remain:
                    #----------Recipient, Command,Arguments, Sender
                    'message' : id + ' Searching server'
                })
                print "Finding a match for: " + id
        elif cmd == "Confirm":
            if matchListing[id] == arg1:
                pubnub.publish({
                    'channel' : 'MBPocketChange',
                    #Message order is, and should remain:
                    #----------Recipient, Command,Arguments, Sender
                    'message' : arg1 + ' FoundCOp,' + id + ' server'
                })
                matchListing[arg1] = id
            else:
                pass #Cheater.
        elif cmd == "SConfirm":
            if matchListing[id] == arg1 and matchListing[arg1] == id:
                os.system('python server.py MBPocketChange' + arg1)
                #Here, the argument tells both players what room to join.
                #The room is created from the first player's ID.
                pubnub.publish({
                    'channel' : 'MBPocketChange',
                    #Message order is, and should remain:
                    #----------Recipient, Command,Arguments, Sender
                    'message' : id + ' GameStart,' + arg1 + ' server'
                })
                pubnub.publish({
                    'channel' : 'MBPocketChange',
                    #Message order is, and should remain:
                    #----------Recipient, Command,Arguments, Sender
                    'message' : arg1 + ' GameStart,' + arg1 + ' server'
                })
            else:
                pass #hax
    else:
        pass


def connected():
    pass

try:
    channel = sys.argv[1]
    server = False
    print("Listening for messages on '%s' channel..." % channel)
    pubnub.subscribe({
        'channel'  : channel,
        'connect'  : connected,
        'callback' : receive
    })
except:
    channel = "MBPocketChange"
    server = True
    print("Listening for messages on '%s' channel..." % channel)
    pubnub.subscribe({
        'channel'  : channel,
        'connect'  : connected,
        'callback' : receive
    })

tornado.ioloop.IOLoop.instance().start()

1 个答案:

答案 0 :(得分:2)

如果您运行此错误消息:

string.split(nil, ' ')

仔细检查您的输入以确保您确实传入了一个字符串。

编辑:特别是,msglist[0]不是Lua数组中的第一个位置,Lua数组从1开始。

顺便说一句,这个函数是在你使用结肠复合糖的意图时编写的,例如:

msglist=msg:split(' ')