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

The size of an array

Just as you are able to find the length of a string with the # operator, you can find the length of an array as well. This is because the # operator gives back the size of a table. We can use this operator to loop through an array that has a dynamic size, for example, such as the following code snippet:

arr = { "a", "b", "c", "d", "e", "f", "g" }length = #arr print ("array length: " .. length)for i=1,#arr do  print (arr[i])end

The length operator # will only count array elements starting from index 1. This means if you use index 0, it will not be counted towards the number of elements in the array:

arr = { }arr[0] = "x" -- not counted towards lengtharr[1] = "y"arr[2] = "z"length = #arr -- length = 2!print ("array length: ...
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