|
| 16 Nov 2016 11:13 AM |
Is this the proper way to do linked lists in Lua
list = {'a',{'b',{'c',{'d'}}}} |
|
|
| Report Abuse |
|
|
Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
|
| 16 Nov 2016 11:17 AM |
list = {'a',{'b',{'c',{'d'}}}}
print(list[1]) >a print(list([2][1]) >b print(list([2][2][1]) >c print(list[2][2][2][1]) >d
|
|
|
| Report Abuse |
|
|
Soybeen
|
  |
| Joined: 17 Feb 2010 |
| Total Posts: 21462 |
|
|
| 16 Nov 2016 11:18 AM |
I mean, if you need to store an array within an array within an array within an array, by all means do that. Otherwise you might find it helpful to leverage ROBLOX's OOP structure and create an actual hierarchy of Value objects to reference.
|
|
|
| Report Abuse |
|
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 16 Nov 2016 11:23 AM |
if you are talking about 2d arrays then simply
local list = { a = { b = { c = { d = { 'hi', }, }, }, }, }
print(list.a.b.c.d) --> hi
or
print(list['a']['b']['c']['d']) --> hi
|
|
|
| Report Abuse |
|
|
chimmihc
|
  |
| Joined: 01 Sep 2014 |
| Total Posts: 17143 |
|
|
| 16 Nov 2016 11:48 AM |
local list = {value = 5} list = {value = 3, next = list} list = {value = 8, next = list} list = {value = 1, next = list} list = {value = 7, next = list}
print(list.value) print(list.next.value) print(list.next.next.value) print(list.next.next.next.value) print(list.next.next.next.next.value)
|
|
|
| Report Abuse |
|
|
|
| 16 Nov 2016 12:42 PM |
| Linked lists are better than Lua arrays for iteration and removal |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 16 Nov 2016 04:12 PM |
"Linked lists are better than Lua arrays for iteration and removal" Uhm, no. Iteration is slightly faster using a regular Lua array obviously. |
|
|
| Report Abuse |
|
|
|
| 03 Dec 2016 03:56 PM |
^ (late post)
Maybe you are talking about indexing? Because incrementing index and dereferencing takes more steps than dereferencing itself, as done with lists. |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 03 Dec 2016 04:10 PM |
"Maybe you are talking about indexing? Because incrementing index and dereferencing takes more steps than dereferencing itself, as done with lists." no. Indexing is also faster with arrays. Linked-lists are only (theoretically) better with insertion and removal but that's not always true anymore. |
|
|
| Report Abuse |
|
|