Scripts / LUA: Store table name in a table?

Author
Message
Moshroom
9
Years of Service
User Offline
Joined: 18th Jul 2015
Location: Finland
Posted: 14th Dec 2015 13:07 Edited at: 14th Dec 2015 13:07
Good evening, my friends. I'm currently working with a database reader script and since I've got a lot of data to read, 18 almost identical if-else containing for statements has turned my code annoyingly un-elegant. Since there is no case statement in LUA, I'm wondering if there is a way to store name of a table inside another table? Doing so would spare me almost 170 lines of code. For example. If I've got tables named Fruits, Vegetables and Meats and I want to read the lists from files.

The only way I've got this to work so far is to create separate for statements for each "Datasets" (lots of self-repeating code). One compromise would be to create an if-else inside for, but that would still mean numerous lines of code. Another one would be to define the tables differently, but then I would need to change the names to various places in 13 already functional scripts.
smallg
Community Leader
19
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 14th Dec 2015 21:38
i'm not sure if i'm understanding but do you mean like
file = {"a", "b", "c"}
for a = 1,5 do
file[a] = {"v", "w", "x", "y", "z"}
end

then you can use file[1][3] to read file named "a" and it's value "x"?
life\'s one big game

windows vista ultimate

i5 @3.3ghz, 4gb ram, AMD R9 200 series , directx 11
Moshroom
9
Years of Service
User Offline
Joined: 18th Jul 2015
Location: Finland
Posted: 14th Dec 2015 22:13
Not exactly. I have files that have multiple table entries inside of them (each row represents a table entry). I'm wondering what would be the simplest (least self-repeating) possible function to read the files and store the first 10 rows from them to tables (one table per file). Unfortunately I can't make tables inside a table because then I would have to change the way my existing scripts read those tables (more than 4000 lines of code and part of that published in the store or this forum). My idea was to create a table to store filenames and another to store the names of the tables where to store the data from the files after reading them. Kind of like links. If that was possible I could just create one for statement to do the trick. Unfortunately I've come to conclusion that it might be impossible.

My second best solution is to use single for to read the files but after that an if else statement repeated 18 times to store the data to tables. It works ok, but is cumbersome if I need to modify it later.
duramin
9
Years of Service
User Offline
Joined: 25th May 2015
Location:
Posted: 17th Dec 2015 13:55 Edited at: 17th Dec 2015 13:57
lil stupid question: why don't you use multidimensional arrays?

like:



so you can later get your stuff with


Will that work for you? or did i completely misunderstood your problem?

Code is completely untestet, btw
PM
3com
10
Years of Service
User Offline
Joined: 18th May 2014
Location: Catalonia
Posted: 17th Dec 2015 18:11 Edited at: 17th Dec 2015 18:12
Quote: "My second best solution is to use single for to read the files but after that an if else statement repeated 18 times to store the data to tables. It works ok, but is cumbersome if I need to modify it later."


Maybe using a single function, and passing it all the necessary values as args, IE:

function myfunc (filename, dt, etc)

end

so you call the func as :

myvalue = myfunc("fruits.dat",2)

Or maybe, I'm missing something.

3com
Laptop: Lenovo - Intel(R) Celeron(R) CPU 1005M @ 1.90GHz

OS: Windows 10 (64) - Ram: 4 gb - Hd: 283 gb - Video card: Intel(R) HD Graphics

PM
Moshroom
9
Years of Service
User Offline
Joined: 18th Jul 2015
Location: Finland
Posted: 17th Dec 2015 18:18
Hi. I don't use that because I've already written more than 4000 lines of code that call the original tables. If I switched to multidimensional arrays now I would have to change the way the tables are called in each individual call. In other words, it would take more time than writing 18 if else statements.
duramin
9
Years of Service
User Offline
Joined: 25th May 2015
Location:
Posted: 17th Dec 2015 18:49 Edited at: 17th Dec 2015 20:15
So, if i understand you right, you need to set the tabel name as variable

to do so, you can try



i don't know if this is supported in Game Guru and its the only way i know how to do this in lua.

so you may try:


can't test this as i'm not on my GG-Computer
AND: never tried this with tables..

Edit: if it don't work with tables, you can also try:


PM
duramin
9
Years of Service
User Offline
Joined: 25th May 2015
Location:
Posted: 17th Dec 2015 20:35 Edited at: 17th Dec 2015 21:01
Sorry, that i don't edit the Last post, as this is tested and working



i used this as testcode as i am to lazy to build up 3 files with 10 entrys ^^
so, with this code you CAN set the tablename via a variable.
PM
Moshroom
9
Years of Service
User Offline
Joined: 18th Jul 2015
Location: Finland
Posted: 18th Dec 2015 06:42
Interesting. I'll try whether that works when I get to my PC.

Login to post a reply

Server time is: 2024-12-22 10:33:12
Your offset time is: 2024-12-22 10:33:12