Lua 尝试为数值编制索引

Lua 尝试为数值编制索引,lua,Lua,我试图用远程服务器上可用的每个播放列表的UUID创建一个数组,但我在播放列表UID[I]=string.sub(messagestring,I*16,I*16+16)处获得了一个尝试索引数值(全局“playlishuid”)错误 十六进制到字符串转换后的典型消息字符串是:060e2b34020501000e1001010103020008300002d00000000000002000000010ad17fc696b49454db17d593db3de5553e59bf5455689ed4c01

我试图用远程服务器上可用的每个播放列表的UUID创建一个数组,但我在播放列表UID[I]=string.sub(messagestring,I*16,I*16+16)处获得了一个尝试索引数值(全局“playlishuid”)错误


十六进制到字符串转换后的典型消息字符串是:
060e2b34020501000e1001010103020008300002d00000000000002000000010ad17fc696b49454db17d593db3de5553e59bf5455689ed4c019731c6dd3c071f0e00

我想您需要一个额外的变量
播放计数

如果用
math.floor
覆盖变量
playlisuuid
,则
playlisuuid
是一个数字。稍后,当您编写
playlysuid[i]
时,您试图引用一个表,但
playlysuid
的类型是
number
。因此,由于类型不正确,您从Lua获得一个执行错误

GetSPLListResponse = "030200"

-- Playlists container
playlistsUUID = {}
playlistname = {}

-- TCP connection to the server
address = Properties["IP_Address"].Value
port = 11730
sock = TcpSocket.New()
sock.ReadTimeout = 0
sock.WriteTimeout = 0
sock.ReconnectTimeout = 5

sock.EventHandler = function(sock, evt, err)
    if evt == TcpSocket.Events.Connected then
        print("Server connected")
    elseif evt == TcpSocket.Events.Reconnect then
        print("Server reconnecting...")
    elseif evt == TcpSocket.Events.Data then
        print("Server has data")
        print("Socket buffer lenght: " .. sock.BufferLength)
        buflen = sock.BufferLength
        message = sock:Read(buflen)
        messagestring =      -- convert HEX message to String
              message:gsub(
              '.',
              function(c)
                return string.format('%02X', string.byte(c))
              end                  
              )
              print("Message after HEX to String " .. messagestring)
        
        if string.find(messagestring, GetSPLListResponse) then -- GET SPL UUID LIST
            print("GetSPLList Response received")
            playlistsUUID = math.floor((#message - 16) / 16) -- calculating the amount of playlists
            print("Playlists calculated: " .. playlistsUUID)
            
            for i = 1, playlistsUUID do -- putting each playlist data in a array
                playlistsUUID[i] = string.sub(messagestring, i * 16, i * 16 + 16) --chops the data into 16byte pieces into the array
            end
            Controls.PlaylistBox.Choices = playlistsUUID

您的代码很奇怪,因为您使用变量
playlisuuid
存储播放列表表(您想要的结果,用
{}
初始化),并且还使用相同的变量
playlisuuid
作为计数器(
playlisuuid=math.floor
)。您可能需要一个额外的变量
playlycount
。很抱歉Pigger打扰了您和其他高级用户:我已经尝试自己解决这个问题,我已经阅读了这里的所有其他类似问题以及更多页面(以及Lua指南)。不幸的是,这是我第一次尝试Lua(我有一点PyHon经验……),我仍然不知道你认为明显的事情…此外,代码的核心部分是由一位为生产系统的公司工作的开发人员编写的,该系统将在其中使用。。。
GetSPLListResponse = "030200"

-- Playlists container
playlistsUUID = {}
playlistname = {}

-- TCP connection to the server
address = Properties["IP_Address"].Value
port = 11730
sock = TcpSocket.New()
sock.ReadTimeout = 0
sock.WriteTimeout = 0
sock.ReconnectTimeout = 5

sock.EventHandler = function(sock, evt, err)
    if evt == TcpSocket.Events.Connected then
        print("Server connected")
    elseif evt == TcpSocket.Events.Reconnect then
        print("Server reconnecting...")
    elseif evt == TcpSocket.Events.Data then
        print("Server has data")
        print("Socket buffer lenght: " .. sock.BufferLength)
        buflen = sock.BufferLength
        message = sock:Read(buflen)
        messagestring =      -- convert HEX message to String
              message:gsub(
              '.',
              function(c)
                return string.format('%02X', string.byte(c))
              end                  
              )
              print("Message after HEX to String " .. messagestring)
        
        if string.find(messagestring, GetSPLListResponse) then -- GET SPL UUID LIST
            print("GetSPLList Response received")
            PlaylistCount = math.floor((#message - 16) / 16) -- calculating the amount of playlists
            print("Playlists calculated: " .. PlaylistCount)
            
            for i = 1, PlaylistCount do -- putting each playlist data in a array
                playlistsUUID[i] = string.sub(messagestring, i * 16, i * 16 + 16) --chops the data into 16byte pieces into the array
            end
            Controls.PlaylistBox.Choices = playlistsUUID