Corona – Arrays

The Array

Let’s talk Arrays. In any language an Array is variable that holds a list of values. Array sounds so technical. Just call it a list, everyone understands a list. Here’s an example:

{ "Apples", "Bananas", "Cherries" }

Note the syntax. The whole thing is wrapped in the { (left curly brace) and the } (right curly brace). These define the extent of the list. Each list item is separated by a comma (,).

Make an array

Try it yourself. Make a new Corona project and type the following into main.lua. Below I have defined a local variable and set it equal to an array.

local fruit_array = { "Apples", "Bananas", "Cherries" }

Testing the Array

Test the array for yourself. Use the print function to display the array and other results to the terminal window. Try this:

print( fruit_array )

The results here are not readable by humans.

Concat

Use table.concat() to convert the array’s contents into a single string. Try this:

print( table.concat( fruit_array ) )

This time the terminal displays the contents of the array. It’s still hard to read. The table.concat() method provides the option to add a separator between each of the array elements. Try this:

print( table.concat( fruit_array, ", " ) )

This time each element is printed with a comma and a space between.

Index

Arrays are organized by index. This is the same way that you might use a list in real life, giving each item in the list a number. The first item in the list is number 1. Or you could say the first item is located at index 1. Apples in the example. Bananas is at index 2.

Accessing values in an array

Here we’re accessing an element in the array by index. The syntax is to follow the array name with the [ (left square bracket) and the ] (right square bracket) around the index.

print( fruit_array[1] ) // should print Apples

Try this:

print( fruit_array[2] ) // Bananas

What have we learned so far.

  1. An array can hold any number of values.
  2. Define the array as a list of values between the { and the }
  3. each value is separated by a comma.
  4. Values in an array are accessed by their index.

What happens if you use an index that is out of range. Our array has three items, here we try to access item 5.

print( fruit_array[5] ) // nil

Hmmm, nil? Well I guess that’s OK there’s nothing at index 5. Nil is a special value that represents nothing.

Counting the items in an array

Often you will want to know how many things are in a list. Try this:

print( #fruit_array ) // should display 3

Adding elements to an array

Sometimes you’ll need to add an element to an existing Array. Use the following method:

table.insert( fruit_array, "Durian" )

Test the results with:

print( fruit_array[4] )

The table.insert() method adds a new element to the end of an array.

To update, edit, change the value in an array use the following syntax:

fruit_array[4] = "Date"

Here we’ll swap Durian for Date. The syntax here essentially says set the value at index 4 in the fruit_array to “Date”.

This method can also be used to add elements to an array at any index. The following adds a new item at index 5.

fruit_array[5] = "Elderberry"

Since there wasn’t anything at index 5, a new element is added to the array at that index.

Combine this with # to count the array and you have a syntax for adding an element at the of an array. For example:

fruit_array[ #fruit_array+1 ] = "Fooberry"

This works just fine, but I prefer the table.insert() method as it reads more clearly.

Why would you ever want to keep track of fruit in an Array? You might make a game with different types of fruit, or a shopping list of fruit, or a healthy snack randomizer. Or Fruit Flash cards iPhone thing for studying botany? Then again you may never keep track of fruit in an array, on the other hand…

You might, make a list of display objects. Many programs need to keep track of things that are being displayed on the screen. This is much easier than make a new variable for each item.

Speaking of which, a single array can store as many items as you might need, with one name for them all. Which makes arrays convenient for situations where your not sure of of many items you might be working with.

You can put anything in an array. Numbers, Strings, objects, tables, other arrays.

Mix and match. One array can store any combination of various elements.

More info:

This is a link to general info on Arrays in Lua:

http://www.lua.org/pil/11.1.html

Here’s some Corona Specific info on Arrays:

http://developer.anscamobile.com/reference/arrays

 

Leave a Reply

Your email address will not be published. Required fields are marked *