Sort table with gaps - lua

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

Related

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.

Attempting to edit array that contains specific key

So I am trying to edit my config list where it has to edit robbed to true when entity is equal to entity in the list (entities get generated when my script is starting)
Config file
Config.location = {
[1] = {
x = 24.39,
y = -1345.776,
z = 29.49,
h = 267.58,
robbed = false,
entity = nil
},
[2] = {
x = -47.7546,
y = -1759.276,
z = 29.421,
h = 48.035,
robbed = false,
entity = nil
},
}
So this list gets loaded - When [1] has been robbed it should change robbed in [1] if the entity matches.
I would imagine i should do a for loop but i'm still clueless.
As Config.list is a sequence with positive integer keys starting from 1 you can conveniently use the iparis iterator in combination with a generic for loop to check every entry in your list.
for i,v in ipairs(Config.location) do
v.robbed = v.entity == someOtherEntity and true or false
end
Of course your entity entries shouldn't be nil as this wouldn't make sense.

Insert Additional data into Lua table at index

I have a table called "inventory", initialized like so:
inventory = {}
inventory[1] = { qty = 0 }
I want to add more data to this table, at the index 1, eg:
val = { id = "example" }
inventory[1] = inventory[1], val
Is there a way I can do this while preserving the data that is already in this table at this index?
The final result should be something like:
inventory[1] = { qty = 0, id = "example" }
But if I try to print the id after trying this code I get:
print(inventory[1].id) == Nil
inventory[1].id = "example"
or
inventory[1]["id"] = "example"
or
this other SO answer with first_table being inventory[1] and second_table being val.
FWIW, you'd need 2 variables on the left side of the expression for inventory[1] = inventory[1], val to work: a, b = x, y.
You need to take the first key in the table and use it:
local inventory = {}
inventory[1] = { qty = 0 }
local val = { id = "example" }
--
local KeyName = next(val)
inventory[1][KeyName] = val[KeyName]
print(inventory[1][KeyName])
-- or
print(inventory[1].id)

World of Warcraft Lua - sort table

I'm trying to sort a table for a addon/weakaura but I do not see how to do it ( sort by the attribut value).
Example :
player = {
value = 34
class = Warrior,
id = 1
},
{
value = 1,
class = mage,
id = 2
},
{
value = 3443,
class = Paladin,,
class = 3
}
I want :
player = {
value = 1,
class = mage,
id = 2
},
{
value = 34
class = Warrior,
id = 1
},
{
value = 3443,
class = Paladin,
class = 3
}
Someone an idea how to do this ?
Assuming you fix your table declaration, you can sort the table using a custom function:
local player = {
{ value = 34, class = "Warrior", id = 1 },
{ value = 1, class = "mage", id = 2 },
{ value = 3443, class = "Paladin", id = 3 },
}
table.sort(player, function(a,b) return a.value < b.value end)
This will sort elements of the player table by the value of the value field. You can come up with a more elaborate sorting condition if needed.
Refer to http://www.lua.org/manual/5.3/manual.html#pdf-table.sort for more information on table.sort

how to access data in such table?

I'm writing a program with lua. I have data that organized in the following way:
t= {
i1 = {
p1 = { value = "i1p1" },
p2 = { value = "i1p2" },
-- etc
pm = { value = "i1pm" }
},
i2 = {
p1 = { value = "i2p1" },
p2 = { value = "i2p2" },
-- etc
pm = { value = "i2pm" }
},
im = {
p1 = { value = "imp1" },
p2 = { value = "imp2" },
-- etc
pm = { value = "impm" }
}
} --(inner tables)
In another way each group of data is indexed by two variables i&p,I am sure that the data is kept correctly but I want a way to print the data from their tables because I won't know the values of i and p to iterate over them or even the numbers n & m any body know how to do this with lua?
If you know the depth of your nested (inner) tables, you can iterate explicitly to that depth:
for k1,v1 in pairs(t) do
for k2,v2 in pairs(v1) do
for k3, v3 in pairs(v2) do
print(k3, ":", v3)
end
end
end
Alternatively, you can recursively iterate into your nested structure:
function print_tbl(tbl)
if type(tbl) == "table" then
for _,v in pairs(tbl) do
print_tbl(v)
end
else
print(tbl)
end
end
print_tbl(t)
This is just an example. If your tables contain functions, contains userdata, or your nesting has cycles, you'll need a different approach. Take a look at table serialization on the Lua user wiki. Serialization requires sensible handling of tables with nesting, functions, cycles, etc. You may be able to use lessons learned on your data.

Resources