Different ways of inserting into a table LUA - lua

Okay, so i come down to a dilemma that I can't seem to solve in LUA.
Basically I am trying to insert values into a table, and i am trying to insert it like this:
activeContracts = {
[user_id] = {
[1] = {
source = src
}
}
}
But not in that style but instead of this style:
activeContracts = {}
activeContracts[user_id][1]["source"] = src
But the last example doesn't work. I've googled around and I can't seem to find any documentation that displays my dilemma.
If anyone experienced in LUA can comment on this it would mean alot.
Regards.

activeContracts = {} creates an empty global table.
activeContracts[user_id][1]["source"] = src attempts to assign scr to activeContracts[user_id][1]["source"] But you may not index activeContracts[user_id][1] because it does not exist.
You may also not index activeContracts[user_id] for the same reason.
So you're trying to assign values to a nested tables that do not exist. You have to create that nested structure first. You're basically trying to enter a room in the third floor of a house you never built.
activeContracts = {[user_id] = {{}}}

Related

Understanding how to access objects in lua

The closest i've gotten to figuring this out, came from this post Understanding how to access values in array of tables in lua which actually has the most useful information i've seen. However, I'm still running into a minor issue that I hope someone could help me make more sense of it.
As the title states, I'm trying to access an object in lua. I've learned that dot notation doesn't work, so the alternative is to use [] brackets. I have this object here that I can't seem to access.
[1] = ▼ {
["CopperOre"] = ▼ {
["Counter"] = 0,
["Earned"] = 0
}
}
This was a paste from the ROBLOX studio console, for those who are familiar with it. This object can easily be seen by calling the object name print(obj)
However, I can't seem to access anything inside of the object. obj.CopperOre returns nil, same with obj['CopperOre']
How exactly do I access the parts of the object?
You are forgetting to pass the index into the obj array to access the object stored there.
So to properly access the CopperOre table, you need to reference it like this :
print(obj[1].CopperOre)
-- or
print(obj[1]["CopperOre"])

How to make a join with two tables with django-tables2

Can anyone provide a clear example of how to show a table using django-tables2 that selects and presents data from two(or more) related models?
I've found lots of posts about that, most of them quite old, and none really a working example.
These are my models:
class Person(models.Model):
name = models.CharField(verbose_name="Name",max_length=50)
fname = models.CharField(verbose_name="F.Name",max_length=50)
class Speech(models.Model):
person = models.ForeignKey(Person, on_delete=models.CASCADE)
said = models.CharField(verbose_name="Said",max_length=50)
I simply want to show a table with columns "Name, F.Name, Said". Which is the best way? And with multiple tables?
Thanks in advance.
Well, nobody answered my question. After digging and trying I found a way to show fields from related models in one table. The thing is the table definition into tables.py should be like this:
class SpeechTable(tables.Table):
name = tables.Column(accessor='person.name')
fname = tables.Column(accessor='person.fname')
said = tables.Column()
class Meta:
attrs = {"class": "paleblue"}
Not sure if this is the best way, but it is simple and works fine.

Lua nested tables, table.insert function

i started learning lua and now i'm trying to deal with nested tables.
Basically i want to create a kind of local "database" using json interaction with lua (i found out that was the best thing to store my values)...
what i supposed to do is to scan all members inside a chatgroup (i'm using an unofficial telegram api) and store some values inside a table. I was able to retrieve all datas needed, so here's the structure declared in main function:
local dbs = load_data("./data/database.json")
dbs[tostring(msg.to.id)] = {
gr_name = {},
timestamp = "",
user = { --user changes into user ids
us_name = {},
us_nickname = {},
us_role = ""
},
}
where msg.to.id contains a valid number. This is what i tried to do:
dbs[tostring(id)]['users'][tostring(v.peer_id)]['us_nickname'] = v.username
this one works but this one:
dbs[tostring(id)]['users'][tostring(v.peer_id)] = table.insert(us_name,v.print_name)
(id is a correct number and matches with first field, same as all values passed like v.peer_id and v.print_name so those are not the problem)
gives error "table expected"... i'm pretty sure i have totally no idea of how to insert an element in such a table like mine.
Can anyone of you be so kind to help me? I hope to be clear enough explaining my issue.
Thanks in advance to everyone :)
To add new user name to an existing user you probably want to insert it into the sub-table like this:
table.insert(dbs[tostring(id)]['users'][tostring(v.peer_id)].us_name, v.print_name)

Creating a table with a string name

I've created a lot of string variable names and I would like to use the names as table names ie:
sName1 = "test"
sName2 = "test2"
tsName1 ={} -- would like this to be ttest ={}
tsName2 ={} -- ttest2 = {}
I can't figure out how to get this to work, have gone through various combinations of [] and .'s but on running I always get an indexing error, any help would be appreciated.
In addition to using _G, as Mike suggested, you can simply put all of these tables in another table:
tables = { }
tables[sName1] = { }
While _G works the same way pretty much every table does, polluting global "namespace" isn't much useful except for rare cases, and you'll be much better off with a regular table.
your question is sort of vague, but i'm assuming you want to make tables that are named based off of string variables. one way would be to dynamically create them as global objects like this:
local sName1 = "test"
-- this creates a name for the new table, and creates it as a global object
local tblName = "t".. sName1
_G[tblName] = {}
-- get a reference to the table and put something in it.
local ttest = _G[tblName]
table.insert(ttest, "asdf")
-- this just shows that you can modify the global object using just the reference
print(_G[tblName][1])

How does this Linked List example in Lua actually work?

I'm in the process of relearning programming after several years and I'm currently focusing on both C# and Lua. The book I'm using for Lua has an example for a Linked List, but I'm having a difficult time understanding exactly how it's working.
list = nil
for line in io.lines() do
list = {next = list, value = line}
end
If i'm reading this right
it's creating a new table
assigning list to that table, setting the "next" key/identifier (correct terminology?) to point to the list (which is still nil at the point of the first created table)
then setting the "value" key/identifier to be whatever was read in
then the "list" is now actually pointing to the newly created table
Then on the next run through of the loop
creating the next table
setting the "next" key/identifier to point to the list (which is now pointing to the previously created table)
then setting the "value" key/identifier to be whatever was read in
then the "list" is now actually pointing to the newly created table...again
I just wanted to be sure I understood exactly how this was working as it seemed a little odd/weird that the list was trying was creating a table and pointing to whatever it was currently pointing to just before the execution of the line completed and the list was updated to point at the newest created table.
Or am I way off here?
This is somewhat similar to what LIFO linked lists are in other languages(like c or c++). Yes, you were following it correctly.
Suppose my inputs are:(in the same order)
21
Hi
35
No
Then, my list is created as:
list = {
value = "No",
next = {
value = 35,
next = {
value = "Hi",
next = {
value = 21
next = nil
}
}
}
}
Essentially what is happening is, inside a for loop, you are creating a table and the next key is nil, initially. In subsequent loops, you are creating a table again and with the list variable you assign it to the next key, which is now a reference to the previous table, and so on.
I'll give you an example.
for i=1,3 do
list = {int=i, next=list}
end
When this executes, the first next key will be nil because list hasn't been defined yet. But in the next table its next key will be a reference to the previous table, and so on. If you try to print list.int and list.next, they would be referring to the table where int=3 because the for loop ends at 3 and thus the variable refers to that part. If you need to change this, you simply change the numeric for loop to something like
for i=3,1,-1 do
So it would actually look like this:
list = {int=1, next=nil}
list = {int=2, next=previousTable}
list = {int=3, next=previousTable}
or
list = {
int=3,
next = {
int=2,
next = {
int=1,
next=nil
}
}
}
Hope this helps.

Resources