Table Scope

I have the following (shortened)

main.lua  
  
1.local objects = {}  
2.local putobjects = require ("putmeinobjects")  
  
putmeinobjects.lua  
  
1.object = "testuser"  
2.table.insert ( objects, object )  

But the object is not inserted into the objects.table.
Any hints what i am doing wrong? Is the Scope wrong? [import]uid: 64502 topic_id: 16674 reply_id: 316674[/import]

you can use it like that, though i cant be sure if it good for you

main.lua
[lua]local obj = require(“obj”)

T = {}

obj.putintable()

for k,v in pairs(T) do
print(k,v)
end[/lua]

obj.lua
[lua]module(…,package.seeall)

local obj = “test”

function putintable()
table.insert(T, obj)
end[/lua]

hope it helps [import]uid: 16142 topic_id: 16674 reply_id: 62329[/import]

thanx m8, will check it out … [import]uid: 64502 topic_id: 16674 reply_id: 62338[/import]

@riebesehl,

yes, you have declared objects as a *local* variable in the first module and you are expecting it to work in the second module. It is definitely out of scope.

as for examples, you have so many variables with the same names that as a beginner you will definitely face issues.

in your putmeinobjects.lua file

local theObjectTable = {}  
  
function theObjectTable:addThisElement(thisElement)  
 table.insert(theObjectTable, thisElement)  
end  
  
return theObjectTable  

in the main.lua

 local obj = require("putmeinobjects")  
  
 local object = "Some test string"  
  
 obj:addThisElement(object)  
  
 local k,v  
 for k,v in pairs(obj) do  
 print(k , " = ", v)  
 end  
  

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16674 reply_id: 62352[/import]

Hey Jayant, do you have how-to (or some sort of tutorial/lesson/write-up) on “k,v in pairs” thingy? I’ve seen it number of times in code snippets (and it’s in my code too), but I sort of put it aside to figure out sometime later when I have time to seek out the answer. I don’t like having things I don’t quite understand in my code. So if you’ve already written something about it that I should read up on, I’d love to. (If you don’t have anything already handy, it’s okay. I’m sure I’ll eventually stumble into it.)

@riebesehl, sorry, I didn’t mean to change the subject, but I just saw the code snippet… and couldn’t help myself.

Naomi [import]uid: 67217 topic_id: 16674 reply_id: 62411[/import]

for k,v in pairs(table) is nothing fancy
its just “for” iterator for table
so explains like that: for every key and value in table do next function(it can be anything, printing their value and other table related things like “if table[i] == 0 then”)

for professional explanation go here:
http://www.lua.org/pil/7.2.html [import]uid: 16142 topic_id: 16674 reply_id: 62436[/import]

Hey Darkconsoles, thank you so much for the explanation and the link. I appreciate it, and I’ll definitely read the write up on lua.org.

Naomi [import]uid: 67217 topic_id: 16674 reply_id: 62438[/import]

you can do all sorts of funny things with this powerful feature, like that:
[lua]local T = {item1 = “one”, item2 = “one”, item3 = “two”, item4 = “two”}
for k,v in pairs(T) do
if v == “one” then
v = “NOT ONE, BUT 10”
end
print(k,v)
end[/lua] [import]uid: 16142 topic_id: 16674 reply_id: 62439[/import]

Thank you, @darkconsoles! I’ll definitely play around with it until I get a real feel for this. Thanks again!

Naomi [import]uid: 67217 topic_id: 16674 reply_id: 62451[/import]

you’re welcome) i found tables in lua are most interesting and most hard to learn things, i dont know why) [import]uid: 16142 topic_id: 16674 reply_id: 62455[/import]