How to insert data and tables into an existing one? - lua

I have a trouble inserting new rows and tables into an already existing one.
Lets call the source SourceFile.lua and its simplified contents:
SourceFile = {};
SourceFile.list = {
BrandName1 = {
number = 10,
products = {
"Product1", 3,
"Product2", 4,
"Product3", 7,
},
},
BrandName2 = {
number = 5,
products = {
"Product1", 10,
"Product2", 3,
"Product3", 6,
},
},
-- and so on
}
I want to do something like this:
require 'SourceFile'
local myData = {
BrandName2 = { -- add new products for the existing brand
products = {
"Product4", 2,
},
},
MyBrandName1 = { -- add new brand
number = 12,
products = {
"MyProduct1", 21,
"MyProduct2", 95,
},
},
-- and so on
}
table.insert(SourceFile.list, myData)
However there's something wrong in my code and I get the following result (printed with inspect):
{
list = { {
BrandName2 = {
products = { "Product4", 2 }
},
MyBrandName1 = {
number = 12,
products = { "MyProduct1", 21, "MyProduct2", 95 }
}
},
BrandName1 = {
number = 10,
products = { "Product1", 3, "Product2", 4, "Product3", 7 }
},
BrandName2 = {
number = 5,
products = { "Product1", 10, "Product2", 3, "Product3", 6 }
}
}
}
What am I doing wrong?
I'm new to lua and pretty sure that it's something obvious, but not for me. Please, help me.
Addition
After these answers I've also found a way to insert new brand names one by one:
SourceFile.list.MyBrandName1 = {
number = 12,
products = {
"MyProduct1", 21,
"MyProduct2", 95,
},
}
This does not fully answer my question, but might be useful to someone new to lua.

table.insert adds its second argument to an array (its first argument). Your SourceFile.list is only supposed to have string keys, so it can't work as an array. You'll need a recursive function to merge the data from one table into the other:
local function meld(data, newData)
for k, v in pairs(newData) do
local oldValue = data[k]
if type(oldValue) ~= 'table' or type(v) ~= 'table' then
-- One of the values is not a table, so let's clobber the old value.
data[k] = v
else
-- Both are tables.
meld(oldValue, v)
end
end
end
meld(SourceFile.list, myData)

You are pushing a table of brandnames into a list of brandnames.
Which makes it a list of brandnames + table with brandnames.
table.insert(SourceFile.list, myData)
This inserts myData to SourceFile.list. myData is a table with brandnames.
SourceFile.list is also a table with brandnames.
List in list.
You have 2 choices to solve this:
Insert each brandname separately
Make a function to merge contents of myData to SourceFile.list

Related

Trying to figure out how to Grab part of a table. (hard to explain fully)

So, what i am trying to do is... well it's best to show. oh and This is with Fivem and trying to modify an existing resource, but...
`BMProducts = {
{
["lostItems"] = {
[1] = { name = "weapon_shotgun", price = 1500, crypto = 2500, amount = 1 },
},
}
}
table.insert(BMProducts, {
["ballasItems"] = {
[1] = { name = "weapon_pistol", price = 1500, crypto = 2500, amount = 1 },
},
})
Config.Products = BMProducts`
And I have a another config, that i need to pull the Correct table, but now sure entirely how
`["products"] = Config.Products["ballasItems"],`
Is what I have but it won't read it, due to what I assume is what i saw when debugging, that when inserting to the table, it assigns a number, ie;
[1] = {lostitems = {... [2] = {ballasItems = {...
One that works, but what my ultimate goal is to make the code plug and play with the table inserts, is this
`BMProducts =
{
["lostItems"] = {
[1] = { name = "weapon_pistol", price = 1500, crypto = 2500, amount = 1 },
},
["ballasItems"] = {
[1] = { name = "weapon_pistol", price = 1500, crypto = 2500, amount = 1 },
},
}`
which works with the config above because the way just above does not assign numbers and not inserting into a table. Any ideas how i can go about setting that config for the correct Products table?
When i try it with the table insert, and with the
`["products"] = Config.Products["ballasItems"],`
It can't find the table, which is due to what i assume the table format being different than what it was, which was the code block at the bottom
so my main thing, is to get
`["products"] = Config.Products["ballasItems"],`
to = the correct table when there is a table insert.
If you don't want to table.insert, then don't table.insert:
BMProducts["ballasItems"] = {
{ name = "weapon_pistol", price = 1500, crypto = 2500, amount = 1 },
}
Now I think you told to not modify the config, so another approach is to just use that additional array index when indexing:
["products"] = Config.Products[2]["ballasItems"]
Which obviously assumes that the config never changes and the entry with ballasItems is on index 2.
If you do not know the index, you may want to iterate over Config.Products and look for the right product.
for i,v in ipairs(Config.Products) do
if v["ballasItems"] then
end
end
You may also consider the case where two or more products match.

Counting elements in nested table by name starting with

I have the table listed below
raids = {
{
T1I0 = {
{'Mightstone of Sargaras', 'Mightstone of Sargaras\n\nMightstone of Sargaras is obtained by farming. GL'}
},
T1I1 = {
{'Blessings Jewel of Elune', 'test'}
},
T1I2 = {
{'Lifegiving Gem of Amanthel', 'test'}
},
T2I0 = {
{'Practicing monster pot', 'test'}
},
T2I1 = {
{'Nuwa stone', 'test'}
}
}
}
I've managed to count the amount of elements T1I0 -> T2I1 = 5 by using the function below, combined with tablelength(raids[1])
function tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
But I'm having some problem with counting only elements starting with T1, sound return 3.
Anyone got an ide how I can modify the last part?
Try this code:
for k in pairs(T) do if k:sub(1,2)=="T1" then count = count + 1 end end
However, consider re-structuring the table in two layers, the first one having keys T1 and T2. After this, the count you want is simply #raids.T1.

Sorting an array of tables

This may be easier than I'm making it on myself. I am relatively new to Lua, but experienced in other languages.
I have a table that looks like this:
local state = {}
state[1] = {
show = true,
changed = true,
progressType = "static",
value = 0,
total = 9,
name = "nine",
}
state[2] = {
show = true,
changed = true,
progressType = "static",
value = 0,
total = 7,
name = "seven",
}
state[3] = {
show = true,
changed = true,
progressType = "static",
value = 0,
total = 8,
name = "eight",
}
state[4] = {
show = true,
changed = true,
progressType = "static",
value = 0,
total = 6,
name = "six",
}
What I need to do is sort each table[] entry based on the value of table.value5. I can't find any functions in the docs that expressely say they do more than just a basic table.sort so I find myself a bit stuck. Do I need to manually sort by iterating through and creating a new table with the sorted data?
I can't find any functions in the docs that expressely say they do more than just a basic table.sort so I find myself a bit stuck.
I might be misunderstanding your issue, but table.sort is exactly what you need in this case:
local state = {}
state[1] = {
total = 9,
name = "nine",
}
state[2] = {
total = 7,
name = "seven",
}
state[3] = {
total = 8,
name = "eight",
}
state[4] = {
total = 6,
name = "six",
}
-- Use table.sort with a custom anonymous function
-- to specify how to compare the nested tables.
table.sort(state, function (a, b)
return a.total < b.total
end)
for i=1, #state do
print(i, state[i].name)
end
With table.sort you can provide an optional custom function, which can be as easy or complicated as you want it to be. In this case (as per your question) it is enough to simply compare the total values of your table.

Dynamically building subtables in a table

I'm trying to figure out how to dynamically build a series of sub-tables inside a lua table. For example
function BuildsubTable()
local oTable = {}
local name = {"P1","P2"}
for i = 1, 2 do
oTable[i] = {name = name[i], "x" = i + 2, "y" = i + 1}
end
return oTable
end
expected output:
oTable = {
{name = "P1", "x"=3, "y"=2},
{name = "P2", "x"=4, "y"=3}
}
Which obviously doesn't work, but you get the idea of what I'm trying to do. This is a rather simple task but in LUA 5.3 this is proving to be difficult. I cannot find a good example of building a table in this manner. Any solutions or other ideas would be appreciated. In Python I would use a class or a simple dictionary.
Your problem is that you quote the string indices. The generic syntax for declaring a table key inside the table constructor is [<key>] = <value>, for example, [20] = 20 or ["x"] = i + 2.
A shorthand for ["<key>"] = <value>, that is, for string indices that are valid variable names, you can write <key> = <value>, for example x = i + 2.
In your code you use a mix of both and write { ..., "x" = i + 2, ... }. A quick google search shows me that in Python, which you mention, you quote string keys in dictionaries, so you probably mixed that up with Lua?
EDIT: I noticed this a bit late, but you can also use ipairs to iterate the first table and table.insert to insert values:
function BuildsubTable()
local oTable = {}
local name = {"P1","P2"}
for i,name in ipairs(name) do
table.insert(oTable, {name = name, "x" = i + 2, "y" = i + 1})
end
return oTable
end
Use
oTable[i] = {name = name[i], x = i + 2, y = i + 1}
DarkWiiPlayers & lhf's answers are the proper way.
But here is how you can fix your current code if you intend to use a string as a key
function BuildsubTable()
local oTable = {}
local name = {"P1","P2"}
for i = 1, 2 do
oTable[i] = {name = name[i], ["x"] = i + 2, ["y"] = i + 1}
end
return oTable
end
Output
{
[1] = { ['name'] = 'P1', ['x'] = 3, ['y'] = 2},
[2] = { ['name'] = 'P2', ['x'] = 4, ['y'] = 3}
}

casting a List<object> into NancyFx response

I am trying to build an Nancy OData support app using LinqToQuerystring. I got below sample code. it is working for any query url like:
http:/test/?$filter=Recommended eq true
Get["/test"] = _ =>
{
var dict = (IDictionary)Request.Query.ToDictionary();
new List<Movie>
{
new Movie
{
Title = "Matrix (The)",
ReleaseDate = new DateTime(1999, 3, 31),
DurationInMinutes = 136,
MetaScore = 73,
Director = "Wachowski Brothers",
Recommended = true
},
new Movie
{
Title = "There and Back Again, An Unexpected Journey",
ReleaseDate = new DateTime(2012, 12, 14),
DurationInMinutes = 169,
MetaScore = 58,
Director = "Peter Jackson",
Recommended = false
}
}.AsQueryable()
.LinqToQuerystring(dict);
return dict;
}
You can solve this by calling ToDictionary first.
i.e
var dict = (IDictionary<string, object>) Request.Query.ToDictionary();
...
.LinqToQuerystring(dict);
This is probably because of the way LinqToQuerystring handles Dictionary under the hood, outputting them in an intermediate window causes:
(IDictionary<string, object>) Request.Query
{Nancy.DynamicDictionary}
[Nancy.DynamicDictionary]: {Nancy.DynamicDictionary}
Keys: Count = 2
Values: Count = 2
(IDictionary<string, object>) Request.Query.ToDictionary()
Count = 2
[0]: {[one, one]}
[1]: {[two, 2]}
Edit:
Based on your comment I assume you want to ALWAYS return JSON.
If that's the case the way you would do that is to return:
return Response.AsJson(dict);
This will serialize the dictionary as JSON for you.

Resources