Book Image

Corona SDK Mobile Game Development: Beginner's Guide

By : Michelle M Fernandez
Book Image

Corona SDK Mobile Game Development: Beginner's Guide

By: Michelle M Fernandez

Overview of this book

Table of Contents (19 chapters)
Corona SDK Mobile Game Development Beginner's Guide Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Tables


Tables are the proprietary data structure in Lua. They represent arrays, lists, sets, records, graphs, and so on. A table in Lua is similar to an associative array. Associative arrays can be indexed with values of any type, not just numbers. Tables implement all these structures efficiently. For example, arrays can be implemented by indexing tables with integers. Arrays do not have a fixed size, but grow as needed. When initializing an array, its size is defined indirectly.

Here is an example of how tables can be constructed:

1 a = {}    -- create a table with reference to "a"
2 b = "y"
3 a[b] = 10    -- new entry, with key="y" and value=10
4 a[20] = "Monday"  -- new entry, with key=20 and value="Monday"
5 print(a["y"])    -- 10
6 b = 20
7 print(a[b])     -- "Monday"
8 c = "hello"     -- new value assigned to "hello" property
9 print( c )    -- "hello"

You will notice that in line 5, a["y"] is indexing the value from line 3. In line 7, a[b] uses a new value of variable b and indexes...