[Resolved] What is this structure? Newbie Question

Newbie here.

Just reading over some sample code and see a structure I’m not familiar with.

Basically it’s like…
variablePower = { var1 - var2, var1 - var3, var1 - var4}

Is variablePower then an array storing those three values between the {}? Or is that something else?

Thanks for any help! [import]uid: 191855 topic_id: 32890 reply_id: 332890[/import]

If I’m reading it correctly, it’s using existing variables (var1-var4) and creating a new table with the subtractions done in { }.

Since no key is specified, the table is accessed like a typical array, IE: variablePower[1] = (var1-var2).

Basically this is just like an array in C, but since Lua uses tables it is technically a table and new values can still be added to it that would make it not appear/work as an array. Also, all tables/arrays in Lua are 1-indexed, so if you’re used to other C-based languages keep that in mind (coming from C++ it took me a little while to get used to starting at one not zero).

Hope this helps!

-Treb [import]uid: 181948 topic_id: 32890 reply_id: 130681[/import]

@treb.stewart is spot on.

Since we don’t have the values of var1, var2, var3, and var4, I’m going to have to make them up for the purpose of demonstration:

var1 = 10  
var2 = 5  
var3 = 7  
var4 = 9  

In this case, what you’re seeing becomes:

variablePower = { 10 - 5, 10 - 7, 10 - 9}  
-- or  
variablePower = { 5, 3, 1}  

Which ends up being a Lua table using numeric indexes instead of key-value pairs which is like an Array in most languages. The results are:

variablePower[1] = 5
variablePower[2] = 3
variablePower[3] = 1
[import]uid: 19626 topic_id: 32890 reply_id: 130683[/import]

If I’m reading it correctly, it’s using existing variables (var1-var4) and creating a new table with the subtractions done in { }.

Since no key is specified, the table is accessed like a typical array, IE: variablePower[1] = (var1-var2).

Basically this is just like an array in C, but since Lua uses tables it is technically a table and new values can still be added to it that would make it not appear/work as an array. Also, all tables/arrays in Lua are 1-indexed, so if you’re used to other C-based languages keep that in mind (coming from C++ it took me a little while to get used to starting at one not zero).

Hope this helps!

-Treb [import]uid: 181948 topic_id: 32890 reply_id: 130681[/import]

@treb.stewart is spot on.

Since we don’t have the values of var1, var2, var3, and var4, I’m going to have to make them up for the purpose of demonstration:

var1 = 10  
var2 = 5  
var3 = 7  
var4 = 9  

In this case, what you’re seeing becomes:

variablePower = { 10 - 5, 10 - 7, 10 - 9}  
-- or  
variablePower = { 5, 3, 1}  

Which ends up being a Lua table using numeric indexes instead of key-value pairs which is like an Array in most languages. The results are:

variablePower[1] = 5
variablePower[2] = 3
variablePower[3] = 1
[import]uid: 19626 topic_id: 32890 reply_id: 130683[/import]

@robmiracle - Clear, concise, and precise. Nice answer. [import]uid: 110228 topic_id: 32890 reply_id: 130716[/import]

@robmiracle - Clear, concise, and precise. Nice answer. [import]uid: 110228 topic_id: 32890 reply_id: 130716[/import]

Awesome thanks! Will read up more on tables. I have never encountered a language that uses tables instead of arrays. Thanks all! [import]uid: 191855 topic_id: 32890 reply_id: 130809[/import]

Awesome thanks! Will read up more on tables. I have never encountered a language that uses tables instead of arrays. Thanks all! [import]uid: 191855 topic_id: 32890 reply_id: 130809[/import]