O
O
Osklizg2017-04-03 11:47:28
NoSQL
Osklizg, 2017-04-03 11:47:28

How to display table values ​​in LUA?

I slightly reworked the finished script in LUA, which is needed to display information about the Tarantool database.

local log = require('log')
local console = require('console')
local server = require('http.server')

local HOST = '172.27.40.17'
local PORT = 8008

box.cfg {
log_level = 5,
memtx_memory = 1073741824,
wal_max_size = 5,
log = 'tarantool.log',
background = true,
pid_file = '1.pid',
}

console.listen('172.27.40.17:3311')

if not box.space.examples then
     s = box.schema.space.create('examples')
          s:create_index('primary', {type = 'hash', parts = {1, 'string', 2, 'string', 3, 'string'}})

end

function handler(self)
   local id = self:cookie('tarantool_id')
        local ip = self.peer.host
        local data = ''
        log.info('Users id = %s', id)
        if not id then
             data = 'Welcome to tarantool server!'
                 box.space.examples:auto_increment({ip})

 id = box.space.examples:len()
                 return self:render({ text = data}):
                        setcookie({ name = 'tarantool_id', value = id, expires = '+1y' })
        else
                 local count = box.space.examples:len()
                 data = 'В базе имеется ' .. count .. ' кортежей'
                 return self:render({ text = data })
        end
end

httpd = server.new(HOST, PORT)
httpd:route({ path = '/' }, handler)
httpd:start()

Now I want to display not only the number of tuples, but also the values ​​of the tuples themselves. And I change: What does the error give me: Unhandled error: /home/roman/httptarantool.lua:48: attempt to concatenate local 'count' (a table value) I understand that it swears at an inappropriate value, but I can’t understand where in the script to replace the value with a table.
local count = box.space.examples:len()
local count = box.space.examples:select()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vadim Misbakh-Soloviev, 2017-05-01
@mva

1) https://www.lua.org/about.html#name (TL;DR: not LUA, but Lua)
2) You still need to understand what you are doing.
When you do `:select()` you get a table.
And below you are trying to concatenate it (which, in principle, is wrong even for `number`-values, and it would be nice to use `tostring()` in this place. And in the case of a table, you need a loop to display the values.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question