Search code examples
lua

attempt to index a number value


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

I'm trying to create an array with UUID of each playlist available on a remote server, but I obtain a attempt to index a number value (global 'playlistsUUID') error at playlistsUUID[i] = string.sub(messagestring, i * 16, i * 16 + 16)

a typical messagestring after HEX to String conversion is: 060E2B340205010A0E100101010302008300002D000000010000000200000010AD17FC696B49454DB17D593DB3E553E59BF5455689ED4C019731C6DD3C071F0E00


Solution

  • I think you need an additional variable PlaylistCount to make it work.

    If you overwrite the variable playlistsUUID with math.floor, then playlistsUUID is a number. Later, when you write playlistsUUID[i] you are trying to reference a table but the type of playlistsUUID is number. So you get an execution error from Lua because the type is incorrect.

    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