Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Lua Quick Start Guide

You're reading from   Lua Quick Start Guide The easiest way to learn Lua programming

Arrow left icon
Product type Paperback
Published in Jul 2018
Publisher Packt
ISBN-13 9781789343229
Length 202 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Gabor Szauer Gabor Szauer
Author Profile Icon Gabor Szauer
Gabor Szauer
Arrow right icon
View More author details
Toc

Arrays

An array is a contiguous chunk of memory; some programming languages guarantee that the memory will be contiguous. In Lua, an array might use a linear chunk of memory if the following apply:

  • The table has only numeric indices
  • The numeric indices start from one
  • At least half of the indices are not nil
How tables are implemented is dependent on the internals of Lua. This is not something you usually have to concern yourself with, but if you are interested, the topic is described in detail at http://www.lua.org/doc/jucs05.pdf.

For now, assume that an array is defined as a table that is indexed only numerically, starting with index 1. By this definition, the following code demonstrates how to use a table as an array:

arr = {}

arr[1] = "x"
arr[2] = "y"
arr[3] = "z"

for i = 1,3 do
print(arr[i])
end
...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image