Inserting row to table, with values two levels deep - lua

When looping through a table trying to add 'sub properties' I get an error that i'm attempting to index field '?'. I am also sure that count and v.name both contain values.
Send help?
count = 0
local tbl = {}
for k,v in pairs(list) do
if string.find(v.name,"Jim") then
count = count + 1
tbl[count] = count
--below throws an error
--tbl[count]["name"] = {v.name}
end
end

Related

SCRIPT ERROR: #qb-scoreboard-main/server.lua:34: attempt to index a number value (local 'v')

QBCore.Functions.CreateCallback('qb-scoreboard:server:GetPlayersArrays',
function(source, cb)
local players = {}
for k, v in pairs(QBCore.Functions.GetQBPlayers()) do
players[v.PlayerData.source] = {}
players[v.PlayerData.source].permission = QBCore.Functions.IsOptin(v.PlayerData.source)
end
cb(players)
end
)
v is a number value. you cannot index number values. v.PlayerData is an invalid operation.
Find out why you think v is a table and why it is a number instead.
Either make sure it is not a number or that you don't index it.

Searching in Redis evaluating Lua script

I'm trying to search for field value in hash using Lua script and I'm doing something wrong :)
I have key "articles" which is zset holding article IDs and keys article:n where "n" is the article number.
The following script:
local ids = redis.call("zrange", 'articles', '0', '-1')
local ret = {}
for k, id in pairs(ids) do
local row = redis.call("hgetall", "article:"..id)
ret[k] = row
end
return ret
returns this:
1) 1) "slug"
2) "first-title"
3) "title"
4) "first title"
2) 1) "slug"
2) "second-title"
3) "title"
4) "second title"
Than I'm trying to include condition to return only keys containing string "second" in title but it returns nothing.
local ids = redis.call("zrange", 'articles', '0', '-1')
local ret = {}
for k, id in pairs(ids) do
local row = redis.call("hgetall", "article:"..id)
if (string.find(row[4], "second")) then
ret[k] = row
end
end
return ret
Please could you help me?
The table you returned from lua, must be an array whose index staring from 1.
However, In your example, only the second article matches the condition, and its index is 2. So you are, in fact, setting the table as: ret[2] = row. Since the returned table is NOT an array whose index starting from 1, Redis takes it as an empty array, and you get nothing.
Solution:
local ids = redis.call("zrange", 'articles', '0', '-1')
local ret = {}
local idx = 1; -- index starting from 1
for k, id in pairs(ids) do
local row = redis.call("hgetall", "article:"..id)
if (string.find(row[4], "second")) then
ret[idx] = row -- set table
idx = idx + 1 -- incr index by 1
end
end
return ret
Try this condition
if (string.find(row[4], "second") ~= nil) then

Constructing Key/Value Table in Lua

I'm trying to build a script for a MUD I play that will create a table to keep track of average xp for each mob. I'm having trouble with the syntax of checking whether an element in a table exists and if not creating it. I tried something like this but keep getting: attempt to index field '?' (a nil value)
mobz_buried = {
{mob = "troll", quantity = 2}
{mob = "warrior", quantity = 1}
{mob = "wizard", quantity = 1}} -- sample data
number_of_mobz_buried = 4
xp_from_bury = 2000 -- another script generates these values, these are all just examples
xp_per_corpse = xp_from_bury / number_of_mobz_buried
for _, v in ipairs(mobz_buried) do
if type(mobz[v].kc) == "variable" then -- kc for 'kill count', number of times killed
mobz[v].kc = mobz[v].kc + 1 -- if it exists increment kc
else
mobz[v].kc = 1 -- if it doesn't exist create a key value that matches the mobs name and make the kc 1
end
if type(mobz[v].xp) == "variable" then -- xp for average experience points
mobz[v].xp = (((mobz[v].kc - 1) * mobz[v].xp + xp_per_corpse)/mobz[v].kc) -- just my formula to find the average xp over a range of differant buries
else
mobz[v].xp = xp_per_corpse -- if it doesn't exist create the table just like before
end
end
I'm trying to end up with mobz.troll = {kc, xp}, mobz.warrior = {kc, xp}, mobz.wizard = {kc, xp} and the ability to add more key values based off of the names mobz_buried gives me.
Based on extra info from your comments, it sounds like you didn't construct a table for mobz. Try this:
local mobz = {}
for _, v in ipairs(mobz_buried) do
mobz[v.mob] = mobz[v.mob] or {}
mobz[v.mob].kc = (mobz[v.mob].kc or 0) + 1
-- etc...
end

Trying to compare all entries of one table in Lua

Basically I have a table of objects, each of these objects has one particular field that is a number. I'm trying to see if any of these numerical entries match up and I can't think of a way to do it. I thought possibly a double for loop, one loop iterating through the table, the other decrementing, but won't this at some point lead to two values being compared twice? I'm worried that it may appear to work on the surface but actually have subtle errors. This is how I pictured the code:
for i = #table, 1, -1 do
for j = 1, #table do
if( table[i].n == table[j].n ) then
table.insert(table2, table[i])
table.insert(table2, table[j])
end
end
end
I want to insert the selected objects, as tables, into another pre made one without any duplicates.
Let the outer loop run over the table, and let the inner loop always start one element ahead of the outer one - this avoids double counting and comparing objects with themselves. Also, if you call the table you want to examine table that will probably hide the table library in which you want to access insert. So let's say you call your input table t:
for i = 1, #t do
for j = i+1, #t do
if( t[i].n == t[j].n ) then
table.insert(table2, t[i])
table.insert(table2, t[j])
end
end
end
Still, if three or more elements have the same value n you will add some of them multiple times. You could use another table to remember which elements you've already inserted:
local done = {}
for i = 1, #t do
for j = i+1, #t do
if( t[i].n == t[j].n ) then
if not done[i] then
table.insert(table2, t[i])
done[i] = true
end
if not done[j] then
table.insert(table2, t[j])
done[j] = true
end
end
end
end
I admit this isn't really elegant, but it's getting late over here, and my brain refuses to think of a neater approach.
EDIT: In fact... using another table, you can reduce this to a single loop. When you encounter a new n you add a new value at n as the key into your helper table - the value will be that t[i] you were just analysing. If you encounter an n that already is in the table, you take that saved element and the current one and add them both to your target list - you also replace the element in the auxiliary table with true or something that's not a table:
local temp = {}
for i = 1, #t do
local n = t[i].n
if not temp[n] then
temp[n] = t[i]
else
if type(temp[n]) == "table" then
table.insert(table2, temp[n])
temp[n] = true
end
table.insert(table2, t[i])
end
end

How to find sequential items in an array using lua?

I need a piece of code in lua language that can find sequential items in an array that the number of item in the group exceeds a specific nubmer. Example:if I have the array(the numbers won't be in the right order, randomly distributed)->( 2,5,9,10,11,21,23,15,14,12,22,13,24 ) ; there are two sequential groups (9,10,11,12,13,14,15) and (21,22,23,24 ) . I want the first group to be found if the specific number say (4) or more, or I can get the two groups if the number is (3) or less for example.
thanks
The logical way would seem to be to reorder the table and look for gaps in the sequences.
function table.copy(t)
local t2 = {}
for k,v in pairs(t) do
t2[k] = v
end
return t2
end
function groups(org, cnt)
-- Returns a table containing tables containing the groups found
local res = {}
local group = {}
tbl = table.copy(org) -- Prevent reordering of Original Table
table.sort(tbl)
local last = nil
for _,val in ipairs(tbl) do
if last and last + 1 ~= val then
if #group >= cnt then
table.insert(res,group)
end
group = {}
end
table.insert(group,val)
last = val
end
if #group >= cnt then
table.insert(res,group)
end
return res
end
local org = { 2,5,9,10,11,21,23,15,14,12,22,13,24 }
local result = groups(org,3)
print('Number of Groups',#result)

Resources