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:12

the second "list" is evaluated to the current value of "list", then "list" is rebound with = to the table containing the old value.

It's not so different from i = i + 1

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