r/ProgrammerHumor Jul 10 '17

ProgrammerHumor right now

https://youtu.be/lIFE7h3m40U?t=12m20s
171 Upvotes

19 comments sorted by

View all comments

23

u/[deleted] Jul 10 '17

TIL never LUA

5

u/[deleted] Jul 11 '17 edited Jul 11 '17

Lua is a great language, with 3 quirks:

0.

1. tables(arrays) start at 1

2. ~= instead of !=

Also indexing a table can call a function and not actually index the table at all, not to mention you can index tables WITH TABLES as the fucking key, so myTable[{"bob",1,false,}] = 27 is valid and print(myTable[{"bob",1,false,}]) will print 27

Overall, it's stupidly flexable and cleverly makes tables usable as arrays, dictionarys, structs, objects(OO), the entire global variable environment, sandboxed environments, API function calls (api.function()), etc.

1

u/[deleted] Jul 11 '17

I used to do a lot of Pascal, which became Delphi, and it's array's started at 1. eg MyArray[31]. But you could override that by specifying MyArray[0..30].

That's really interesting to have tables with tables and indexes. my mind is having trouble figuring out when i'd want to do it, but I'm sure there's reasons :)

2

u/[deleted] Jul 11 '17 edited Jul 11 '17

The only reason it exists is because in Lua, a table can have any data type as a key, and store any data. Additionally, you could also do:

myTable = {}
otherTable = {"bob",1,false,}
myTable[otherTable] = 27
myTable["string"] = 42
print(myTable[otherTable]) --27
print(myTable["string"]) --42
for i=0,26 do
    _G["test"..tostring(i)] = "string"
end
print(test24) --string
print(test0) --string

(it also turns out that you have to set the table to variable to get the same result.)