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.
Related
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.
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
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}
}
I'm working on a game where a bunch of characters will be generated on the fly, based on some constraints defined either in the project or externally via mod files. I am using MoonSharp Lua (5.2) interpreter for interfacing with my C# code, and Lua tables to store the constraint presets. As an example:
require "Defaults"
AgePresets = {}
-- Single value
AgePresets.Newborn = 0
-- Simple ranges
AgePresets.Default = defaultAgeRange --referring to the Defaults require
AgePresets.Child = {1, 12}
AgePresets.Teenager = {13, 19}
AgePresets.YoungAdult = {20, 29}
AgePresets.Adult = {30, 40}
AgePresets.MiddleAge = {40, 60}
AgePresets.Senior = {61, 80}
AgePresets.Elder = {81, 99}
AgePresets.Methuselah = {100, 150}
AgePresets.Methuselah2 = {150, 200}
-- Weighted ranges // again referring to previously defined elements to keep things concise
AgePresets.Tween = {
{weight = 1, minmax = AgePresets.Teenager },
{weight = 1, minmax = AgePresets.YoungAdult }
}
This works fine, but from an end-user point of view, there's a lot of unnecessary typing involved. We are clearly working on AgePresets here but it is still mentioned as a prefix before every member name.
I could of course define AgePresets as an array, like AgePresets = { Child = {}, Teenager = {} } but the problem with that is then I cannot refer to previously defined elements in the array.
This doesn't work:
AgePresets = {
Child = {1,12},
RefToChild = Child, //attempt to index a nil value exception
Teen = {13,19}
}
What I ideally want to achieve is a clean, concise way for users to enter this data in, like in the first example but without having to put AgePresets. prefix before everything. How do I go about declaring a scope in a file such that all succeeding members defined in the file will be within that scope, while maintaining the ability to refer to other members defined previously in the scope?
AgePresets = setmetatable({}, {__index = _G})
do
local _ENV = AgePresets
Newborn = 0
Child = {1,12}
RefToChild = Child -- this ref is Ok
Teen = {13,19}
YoungAdult = {20,29}
Tween = {
{weight = 1, minmax = Teen },
{weight = 1, minmax = YoungAdult }
}
rnd = math.random(10) -- global functions are available here
end
setmetatable(AgePresets, nil)
You can mix the two styles: table constructor for fields that don't need to reference variables that aren't in scope yet, followed by assignment statements for the rest.
I would do that unless the order of the fields in the code significantly enhanced comprehension.
I got a table which is not meant to be sorted in a way I need it to be sorted at a specific point.
Thus, I cannot sort the table while creation but have to sort it when needed.
Problem is, there are plenty gabs in the indeces and the Values I want to sort here are nested.
Simplified model:
table = {
[1] = { a = 1 , b = 31231, c = { c1 = "foo" , true } },
[8] = { a = 2 , b = 5231 , c = { c1 = "bar" , true } },
[92] = { a = 8 , b = 2 , c = { c1 ="asdgköbana" , false } },
}
Now I want to sort this table by length of c[1].
How can I do that in the fastest way? Length of table in first dimension will stay under 100 entries.
Indices don't need to be kept. So by a table with 3 entries, it's okay when last index is [3] after the portage. Basicly in this case, I only use the index to identifies neighbors, they have no prior use.
Using table as a variable kills the table library, which you need to get the sort function.
Try the code below. Note that it makes a new table to hold the sorted list but reuses the internal tables.
local t = {
[1] = { a = 1 , b = 31231, c = { c1 = "foo" , true } },
[8] = { a = 2 , b = 5231 , c = { c1 = "bar" , true } },
[92] = { a = 8 , b = 2 , c = { c1 ="asdgköbana" , false } },
}
local s = {}
for k,v in pairs(t) do
s[#s+1]=v
end
table.sort(s,function (a,b)
return #a.c.c1 < #b.c.c1
end)
for k,v in ipairs(s) do
print(k,v.a,v.c.c1)
end