1

I execute Lua under Redis. I face an issue which is that I can't use a string as an array key. My coding is like following, and we found that mytable["wow"] is discarded:

FileName: hget.lua

local mytable = {}
mytable[1]= "Lua"
mytable["wow"] = "Tutorial"
return mytable

Command:redis-cli --eval hget.lua   

Result returned is:

1) "Lua"

1 Answer 1

3

You CANNOT have a string key for the table, if you want to return the table to Redis.

Redis takes the returned table as an array, whose index starting from 1. It discards other elements of the table whose keys are NOT integers. In your case, i.e. mytable["wow"] = "Tutorial", since the key is a string, Redis ignores this element.

Also, the indexes must be sequential, otherwise, Redis discards some elements. Take the following as an example:

local t = {}
t[1] = "1"    -- OK
t[2] = "2"    -- OK
t[4] = "4"    -- since index 3 is missing, this element will be discarded
t["string_key"] = "value"   -- since the key is string, this element will be discarded

return t

Result:

./redis-cli --eval t.lua 
1) "1"
2) "2"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.