Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Linked Lists in Lua

Name: Anonymous 2011-09-14 0:03

Alright, I'm trying to create a linked list in Lua, but I'm having a slight issue. I know how to do it, but not why it works.

Can someone explain the following line of code to me?

list = {next = list, value = v}

To create a linked list I would do something like the following.

for i = 1,5 do
    local v = io.read()
    list = {next = list, value = v}
end

Then to show it I would go.

do
   local l = list
   while l do
       print(l.value)
       l = list.next
   end
end

One again, all of it makes sense except the following.

list = {next = list, value = v}

If anyone here knows how this works and can explain it that would be great.

Name: Anonymous 2011-09-14 0:18

>>2
Okay, so because the right side of the assignment is evaluated first the old table address is saved inside the table and a new object is created for the variable being assigned. If it's the first table then next is given a nil value.

Is that right? I think I get it now.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List