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

Table constructor

If you know the values stored in a table at the time of creating the table, you can use the table constructor to assign the values. Just write the key/variable pairs as assignment statements between the curly braces that define the table. By not including strings, the keys are assumed to be strings:

colors = {   red = "#ff0000",   green = "#00ff00",   blue = "#0000ff"}print ("red: " .. colors.red)print ("green: " .. colors["green"])print ("blue: " .. colors.blue)

Non-string keys can be used if the bracket notation is followed within the constructor. The following code shows valid ways to declare table elements in the table constructor:

colors = { r = "#ff0000", ["g"] = "#00ff00", [3] = "#0000ff"}print ("red: " .. colors.r) ...
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