• Announcement: Lua.org now officially recommends this forum as a meeting place for the Lua community

Lists and such (1 Viewer)

Alienhead

Newcomer
Joined
Mar 29, 2022
Messages
5
Reaction score
0
First let me say, the current job project I'm working with has forced me into using LUA, i'm a coder but of other languages. I would like to report that I fell in love with LUA but not the case. Actually I can't wait to get off this project.

In any other language I've used I have been able to create type lists, or classes if you will
An example.

Type car '-- simple class or data type.
field color
field year
end type

Now if I had 20 cars to put into this type I'd simply :

local var = New car
var.color = "yellow"
var.year = 2019

Then if i needed to insert another ....

local var = New car
var.color = "red"
var.year = 2021

and as many as I needed... I could then go through the list with a for next loop or whatever and get what I needed.

for local x = EachIn Car:list
lookitup = x.year '-- blah blah blah
next

Simple type lists, supported by nearly every language in the scene. But yet after a solid week I have yet to find a way to handle this simple routine in LUA.
Any help please? before i end up getting fired. lol.
 

stetre

Member
Rank: II
Joined
Jan 8, 2020
Messages
105
Reaction score
59
Location
Italy
Website
github.com
Yes, Lua (not an acronym) is a very minimal language and some do find it off-putting at first for this reason. But once you get the grasp of it, you really appreciate its minimality, believe me.

As for how to implement data structures other than associative arrays (that's what tables are), you may want to go through the relevant chapter of the book Programming in Lua , which is the ultimate resource for learning Lua. The first edition is available for free here. It's a bit old, but still relevant. Take a look at chapter 11. If your employer agrees to buy you the newest edition instead of firing you, still better :)

That said, making a list of structures is (or can be) as simple as making a table of tables. For example:

Lua:
 -- Create your empty list of cars:
local cars = {}
-- insert a few cars:
table.insert(cars, { color = 'yellow', year = 2019 })
table.insert(cars, { color = 'red', year = 2021 })
table.insert(cars, { color = 'silver', year = 1966 })
-- ...

-- iterate:
for i, car in pairs(cars) do 
   print(i, car.color, car.year)
end

There is much more to it, of course. How you actually would implement such a list depends, on which operations you want to perform on it, on your taste, etc. Take this just as a basic example.
 

Alienhead

Newcomer
Joined
Mar 29, 2022
Messages
5
Reaction score
0
Thank you for clerifying that, coming frmo c# to lua hasn't been easy but I see your point about once getting it down, it does have some freedom..

ty again.
 
Top