Skip to Content
Lua Quick Start Guide
book

Lua Quick Start Guide

by Gabor Szauer
July 2018
Beginner
202 pages
5h 42m
English
Packt Publishing
Content preview from Lua Quick Start Guide

Storing values

Tables store values; a table is a relational data structure. This makes the table similar to a dictionary in other languages. To store a variable in a table, use the following syntax:

table[key] = value

The following example demonstrates how to make a table, store a value with the key x, and how to retrieve that value:

tbl = {}tbl["x"] = 20i = "x"print (tbl["x"])print (tbl[i])

The key of a table can be any type (even another table!), except for nil. This makes the following code valid—hard to read, but valid:

tbl = {}tbl["x"] = 10tbl[10] = "x"print ("x: " .. tbl["x"])print ("10: " .. tbl[10])

If you use a string key for a table, you can access it with the dot syntax. This syntax allows you to access the same data, but is easier ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Beginning Lua Programming

Beginning Lua Programming

Kurt Jung, Aaron Brown
Vim Masterclass

Vim Masterclass

Jason Cannon

Publisher Resources

ISBN: 9781789343229Supplemental Content