[Lua]Table-torial

JonathinJonathin Retired in a hole.
edited October 2012 in Client Help
So I was talking with Senoske about Lua earlier and he mentioned that he didn't understand tables. I wrote him up a little tutorial on tables in Lua. It's far, very far from comprehensive, but it should be able to get some people started. The tabletorial is located on pastebin.


It's unlisted and shouldn't ever expire. I hope it helps at least someone with their table-woes.
I am retired and log into the forums maybe once every 2 months. It was a good 20 years, live your best lives, friends.

Comments

  • Out of curiosity, is there a reason you wrap your keys in square brackets?
  • JonathinJonathin Retired in a hole.
    edited October 2012
    Habit and consistency, I guess. If there's some major significance to it, I'm not sure. I learned how to use tables by myself, so I didn't learn things that people would've learned by being taught by a professional.


    ETA: I just posted this tutorial on my website. Should be easy enough to find.
    I am retired and log into the forums maybe once every 2 months. It was a good 20 years, live your best lives, friends.
  • The bracket format is the most general way to write the keys for Lua tables. Other forms, such as omitting the keys entirely in numbered lists, or just using key=value for one-word string keys, are basically just shorthand. It's a little clunky, but it using it consistently points out the fact that all tables are still the same data type, regardless of the type of key used. And you'll need them if you want to use keys that include spaces or punctiation, like myTable={['first item'] = 'one', ['second item'] = 'two',...}.
  • Or key names that are reserved Lua terms like "not", "for", "function", etc.
  • Just now got around to actually reading through the tutorial. Nice little intro, but a couple quibbles. It would be preferable, on principle, to name your example table something other than "table", since if you actually make a variable called table, you'll overwrite the builtin one and lose access to all your standard table functions (table.size, table.insert, and so on). And your "library" example (the table with string keys) is missing commas.
  • JonathinJonathin Retired in a hole.
    My bad. I'll fix it up on my site (since I can't edit pastebin) in a bit.
    I am retired and log into the forums maybe once every 2 months. It was a good 20 years, live your best lives, friends.
Sign In or Register to comment.