【问题标题】:attempt to index a number value尝试索引一个数值
【发布时间】:2021-05-08 17:07:18
【问题描述】:
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

我正在尝试使用远程服务器上可用的每个播放列表的 UUID 创建一个数组,但我在 处获得 尝试索引数字值 (global 'playlistsUUID') 错误playlistsUUID[i] = string.sub(messagestring, i * 16, i * 16 + 16)

一个典型的messagestring在HEX到String转换后是:060E2B340205010A0E100101010302008300002D000000010000000200000010AD17FC696B49454DB17D593DB3E553E59BF5455689ED4C019731C6DD3C071F0E00

【问题讨论】:

  • 您的代码很奇怪,因为您使用变量playlistsUUID 来存储播放列表表(您想要的结果,用{} 初始化)并且您还使用相同的变量playlistsUUID 作为计数器(playlistsUUID = math.floor)。您可能需要一个额外的变量PlaylistCount
  • 真的很抱歉 Piglet 打扰到您和其他高级用户:我已经尝试自己解决问题,并且我已经阅读了所有其他类似的问题here和更多页面(以及 Lua 指南)。不幸的是,这是我第一次尝试使用 lua(而且我只有一点 pyhton 经验......),我仍然不知道你认为显而易见的事情......此外,代码的核心是由为生产系统的公司工作的开发人员之一......

标签: lua


【解决方案1】:

我认为您需要一个额外的变量 PlaylistCount 才能使其工作。

如果用math.floor 覆盖变量playlistsUUID,则playlistsUUID 是一个数字。稍后,当您编写playlistsUUID[i] 时,您试图引用一个表,但playlistsUUID 的类型是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")
            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

【讨论】:

    猜你喜欢
    • 2014-04-03
    • 2016-05-09
    • 1970-01-01
    • 2015-10-25
    • 1970-01-01
    • 2017-04-22
    • 2019-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多