How to manipulate number in Redis using Lua Script - lua

I am trying to multiply two numbers stored in Redis using the Lua Script. But I am getting ClassCastException. Could someone point out What is wrong in the program
jedis.set("one", "1");
jedis.set("two", "2");
String script = "return {tonumber(redis.call('get',KEYS[1])) * tonumber(redis.call('get',KEYS[2]))}";
String [] keys = new String[]{"one","two"};
Object response = jedis.eval(script, 2, keys );
System.out.println(response);
throws
Exception in thread "main" java.lang.ClassCastException: java.lang.Long cannot be cast to [B
at redis.clients.jedis.Jedis.getEvalResult(Jedis.java:2806)
at redis.clients.jedis.Jedis.eval(Jedis.java:2766)
at com.test.jedis.script.SimpleScript.main(SimpleScript.java:18)

You can't cast a table to a number in lua. What you want is to grab the number of elements in the table instead. You can do this by using the last element point #. Also, I'd highly recommend separating out your Lua script from the rest of your code, so it's cleaner. Your Lua script should look like:
local first_key = redis.call('get',KEYS[1])
local second_key = redis.call('get',KEYS[2])
return #first_key * #second_key
EDIT: Misunderstood the question. OP correctly pointed out he is trying to multiple two numbers stored as strings rather than a table length. In that case:
local first_key = redis.call('get',KEYS[1])
if not tonumber(first_key) then return "bad type on key[1]" end
local second_key = redis.call('get',KEYS[2])
if not tonumber(second_key) then return "bad type on key[2]" end
return tonumber(first_key) * tonumber(second_key)

Related

How to create a tables with variable length with string-like keys in lua

I have a file database. Inside that file I have something like:
DB_A = ...
DB_B = ...
.
.
.
DB_N = ...
I would like to parse the data and group them in lua code like this:
data={}
-- the result after parsing a file
data={
["DB_A"] = {...},
["DB_B"] = {...},
.
.
.
["DB_N"] = {...}
}
In other words, is it possible to create a table inside a table dynamically and assign the key to each table without previously knowing what will be the names of the key (that is something I can figure out after parsing the data from a database).
(Just as a note, I am using Lua 5.3.5; also, I apologize that my code resembles C more than Lua!)
Iterating through your input file line-by-line--which can be done with the Lua FILE*'s lines method--you can use string.match to grab the information you are looking for from each line.
#!/usr/bin/lua
local PATTERN = "(%S+)%s?=%s?(%S+)"
local function eprintf(fmt, ...)
io.stderr:write(string.format(fmt, ...))
return
end
local function printf(fmt, ...)
io.stdout:write(string.format(fmt, ...))
return
end
local function make_table_from_file(filename)
local input = assert(io.open(filename, "r"))
local data = {}
for line in input:lines() do
local key, value = string.match(line, PATTERN)
data[key] = value
end
return data
end
local function main(argc, argv)
if (argc < 1) then
eprintf("Filename expected from command line\n")
os.exit(1)
end
local data = make_table_from_file(argv[1])
for k, v in pairs(data) do
printf("data[%s] = %s\n", k, data[k])
end
return 0
end
main(#arg, arg)
The variable declared at the top of the file, PATTERN, is your capture pattern to be used by string.match. If you are unfamiliar with how Lua's pattern matching works, this pattern looks for a series of non-space characters with zero or one spaces to its right, an equal sign, another space, and then another series of non-space characters. The two series of non-space characters are the two matches--key and value--returned by string.match in the function make_table_from_file.
The functions eprintf and printf are my Lua versions of C-style formatted output functions. The former writes to standard error, io.stderr in Lua; and the latter writes to standard output, io.stdout in Lua.
In your question, you give a sample of what your expected output is. Within your table data, you want it to contain keys that correspond to tables as values. Based on the sample input text you provided, I assume the data contained within these tables are whatever comes to the right of the equal signs in the input file--which you represent with .... As I do not know what exactly those ...s represent, I cannot give you a solid example for how to separate that right-hand data into a table. Depending on what you are looking to do, you could take the second variable returned by string.match, which I called value, and further separate it using Lua's string pattern matching. It could look something like this:
...
local function make_table_from_value(val)
// Split `val` into distinct elements to form a table with `some_pattern`
return {string.match(val, some_pattern)}
end
local function make_table_from_file(filename)
local input = assert(io.open(filename, "r"))
local data = {}
for line in input:lines() do
local key, value = string.match(line, PATTERN)
data[key] = make_table_from_value(value)
end
return data
end
...
In make_table_from_value, string.match will return some number of elements, based on whatever string pattern you provide as its second argument, which you can then use to create a table by enclosing the function call in curly braces. It will be a table that uses numerical indices as keys--rather than strings or some other data type--starting from 1.

LUA: How to Create 2-dimensional array/table from string

I see several posts about making a string in to a lua table, but my problem is a little different [I think] because there is an additional dimension to the table.
I have a table of tables saved as a file [i have no issue reading the file to a string].
let's say we start from this point:
local tot = "{{1,2,3}, {4,5,6}}"
When I try the answers from other users I end up with:
local OneDtable = {"{1,2,3}, {4,5,6}"}
This is not what i want.
how can i properly create a table, that contains those tables as entries?
Desired result:
TwoDtable = {{1,2,3}, {4,5,6}}
Thanks in advance
You can use the load function to read the content of your string as Lua code.
local myArray = "{{1,2,3}, {4,5,6}}"
local convert = "myTable = " .. myArray
local convertFunction = load(convert)
convertFunction()
print(myTable[1][1])
Now, myTable has the values in a 2-dimensional array.
For a quick solution I suggest going with the load hack, but be aware that this only works if your code happens to be formatted as a Lua table already. Otherwise, you'd have to parse the string yourself.
For example, you could try using lpeg to build a recursive parser. I built something very similar a while ago:
local lpeg = require 'lpeg'
local name = lpeg.R('az')^1 / '\0'
local space = lpeg.S('\t ')^1
local function compile_tuple(...)
return string.char(select('#', ...)) .. table.concat{...}
end
local expression = lpeg.P {
'e';
e = name + lpeg.V 't';
t = '(' * ((lpeg.V 'e' * ',' * space)^0 * lpeg.V 'e') / compile_tuple * ')';
}
local compiled = expression:match '(foo, (a, b), bar)'
print(compiled:byte(1, -1))
Its purpose is to parse things in quotes like the example string (foo, (a, b), bar) and turn it into a binary string describing the structure; most of that happens in the compile_tuple function though, so it should be easy to modify it to do what you want.
What you'd have to adapt:
change name for number (and change the pattern accordingly to lpeg.R('09')^1, without the / '\0')
change the compile_tuple function to a build_table function (local function build_tanle(...) return {...} end should do the trick)
Try it out and see if something else needs to be changed; I might have missed something.
You can read the lpeg manual here if you're curious about how this stuff works.

lua table index is null error

I am trying to write a lua switch, based on what I have read so far it seems this is achieved by using a table. So I made a really barebones table but when I try to run it I get an error saying table index is null.
Ultimately what I want is based on different input, this code should call on different lua files. But for now since I am new to this language I figured I would settle for not having that index error.
thanks
#!/usr/bin/lua
-- hello world lua program
print ("Hello World!")
io.write('Hello, where would you like to go?\n')
s = io.read()
io.write('you want to go to ',s,'\n')
--here if i input a i get error
location = {
[a] = '1',
[b] = '2',
[c] = '3',
[d] = '4',
}
location[s]()
Above is the code that I got so far. Below is the error.
~$ lua lua_hello.lua
Hello World!
Hello, where would you like to go?
a
you want to go to a
lua: lua_hello.lua:11: table index is nil
stack traceback:
lua_hello.lua:11: in main chunk
[C]: in ?
The table code is based on this example here: Lua Tables Tutorial section:Tables as arrays
What appears to be the issue is that location[a] is setting location at index a, which is not a string. When you enter a into your input, it gets read as 'a' which IS a string. Index [a] is different from index ['a']. What you want is to replace your assignments so that they're assigned to strings (location['a'] = '1').
As Ihf has said, if you want to print the output then you need to call print(location[s]) because location[s] will simply return the string '1' (or whichever value, just as a string). If you want to assign the numeric value (to use for calculations or etc) then you should use location['a'] = 1.
Alternatively, keep the value as-is as a string and when you attempt to use the value, simply use tonumber().
Example: x = 5 * tonumber(location[s]).
I hope this was helpful, have a great week!
Try
location = {
['a'] = '1',
['b'] = '2',
['c'] = '3',
['d'] = '4',
}
Then that error goes away but is replaced by attempt to call a string value (field '?') because location['a'] is the string '1'.
Perhaps you want
print(location[s])

Lua - get table hex identifier

I want to know how to get the table hex id. I know that doing:
local some_var = {}
print (some_var)
the result is (for instance):
table: 0x21581c0
I want the hex without the table: string. I know that maybe some of you suggest me to make a regular expression (or something similar) to remove those chars, but I want to avoid that, and just get the 0x21581c0
Thanks
This is simpler and works for all types that are associated with pointers:
local function getId(t)
return string.format("%p", t)
end
print("string:", getId("hi"))
print("table:", getId({}))
print("userdata:", getId(io.stdin))
print("function:", getId(print))
print("number:", getId(1))
print("boolean:", getId(false))
print("nil:", getId(nil))
Result:
string: 0x0109f04638
table: 0x0109f0a270
userdata: 0x01098076c8
function: 0x0109806018
number: NULL
boolean: NULL
nil: NULL
In the standard implementation, there is the global 'print' variable that refers to a standard function that calls, through the global variable 'tostring', a standard function described here. The stanard 'tostring' function is the only way to retrieve the hexadecimal number it shows for a table.
Unfortunately, there is no configuration for either of the functions to do anything differently for all tables.
Nonetheless, there are several points for modification. You can create you own function and call that every time instead, or point either of the the global variables print or tostring to you own functions. Or, set a __tostring metamethod on each table you need tostring to return a different answer for. The advantage to this is it gets you the format you want with only one setup step. The disadvantage is that you have to set up each table.
local function simplifyTableToString(t)
local answer = tostring(t):gsub("table: ", "", 1)
local mt = getmetatable(t)
if not mt then
mt = {}
setmetatable(t, mt)
end
mt.__tostring = function() return answer end
end
local a = {}
local b = {}
print(a, b)
simplifyTableToString(a)
print(a, b)
Without complex patterns, you can just search for the first space, and grab the substring of what follows.
function get_mem_addr (object)
local str = tostring(object)
return str:sub(str:find(' ') + 1)
end
print(get_mem_addr({})) -- 0x109638
print(get_mem_addr(function () end)) -- 0x108cf8
This function will work with tables and functions, but expect errors if you pass it anything else.
Or you can use a little type checking:
function get_mem_addr (o)
return tostring(o):sub(type(o):len() + 3)
end
The table id stated by the OP is invalid in the version of Lua I am using (5.1 in Roblox). A valid ID is length 8, not 9 as in your example. Either way, just use string.sub to get the sub-string you are after.
string.sub(tostring({}), 8)
The reason is, 'table: ' is 7 characters long, so we take from index 8 through the end of the string which returns the hex value.

Lua script to return efficient dictionary from Redis HGETALL call

I need to use Redis HMGET from a Lua script and extract specific values in following code.
But redis.call('HMGET', table_key, hkey1, hkey2, ...) return a flat array of {hkey1, val1, hkey2, val2, ...}
To extract values by key I wrote:
local function flat_map_get(flat_map, hash_key)
local i = 1
while flat_map[i] do
if flat_map[i] == hash_key then
return flat_map[i+1]
end
i = i+2
end
end
Of course, as usage grow, multiple calls to this function presented major performance drop.
What is an efficient way to read values from the flat array returned by HMGET?
Or otherwise, to convert the returned value into a proper key-value table?
After some profiling and tests, we found the following function to have good performance and use it to get a proper table.
This save the need to call a getter function for each hash key retrieval.
local function hgetall(hash_key)
local flat_map = redis.call('HGETALL', hash_key)
local result = {}
for i = 1, #flat_map, 2 do
result[flat_map[i]] = flat_map[i + 1]
end
return result
end

Resources