How to save a table item as a variable?

Found myself in another pithole, again…Basically, I have two tables.

local Players {}  
Players[001] = { Name="T-Rex", Owner="1" }  
  
local Owners {}  
Owners[1] = { Name="Richard", Master="Silly" }  

There are many more entries, but the gist is that I list owners by number in the first table because the details in the second table may change. Owner 1 is always Owner 1, but it may be owned by me one day or Sally the next. And it helps in case there are other tables - I only need to rename something once.

Here’s the problem:

-- Variables  
playerID = self.id  
ownerID = Players[playerID].Owner  
  
-- Commands  
print(ownerID)  
print(Players[playerID].Owner)  
print(Owners[ownerID].Name)  

The first two lines work fine (and give the same number, as they should!) But 03 returns nil.

Any ideas? -_-;;;
[import]uid: 41884 topic_id: 13078 reply_id: 313078[/import]

Where you have your define table code, try: [lua]local Players = {}
Players[001] = { Name=“T-Rex”, Owner=1 }

local Owners = {}
Owners[1] = { Name=“Richard”, Master=“Silly” }[/lua]

The changes are:
Inclusion of ‘=’ signs in defining the tables.
Change Owner=“1” to Owner=1.

You should define tables with an equals as shown above. Also, you were attempting to perform arithmetic functions with a string which would’ve created more errors. You can also use the following code if you need to keep the Owner=“1” in a string format for other references.

[lua]Players[001] = { Name=“T-Rex”, Owner=tonumber(“1”) }[/lua]

Luke
[import]uid: 75643 topic_id: 13078 reply_id: 48041[/import]

Shouldn’t you use:

  
Players[1]  
  

Instead of:

  
Players[001]  
  

? [import]uid: 24111 topic_id: 13078 reply_id: 48048[/import]

oskwish: Actually, Corona handles leading zeroes just fine. :slight_smile: I learned to use them once I found out that table.sort() was ordering things like

etc.

…But clearly that was because I was sorting strings and not numbers! (The leading zeroes are superfluous now, I admit, but help add a tiny bit of order…)

obelisk: AGH!! Thank you. It seems I have once again fought and lost against the power of strings. For some reason my brain assumed that even numbers needed the quotes in order to exist in the table!

That being said, is there a particular reason why you say it’s better to declare as

local Table = {}  

Instead of

local Table {}  

(Keep in mind that, at best, all of my code writing style is just badly cribbed from corona sample lua files…) -_-;;; [import]uid: 41884 topic_id: 13078 reply_id: 48080[/import]