Wikimedia

[Lua task #08] Tables in Lua

To work on this, you must have successfully finished the task "[Lua task #07] Repeating code" before!

This task requires independent learning and is more difficult.

Lua has one structure - the table - that is used to store arrays, lists, or sets of values. The values may be any type.

A simple table is demonstrated in https://en.wikipedia.org/wiki/Module:Sandbox/RexxS/GCI08 and it is tested in https://en.wikipedia.org/wiki/User:RexxS/sandboxGCI#Lua_Task_8

1 Make a copy of the function "mum" in your module sandbox. Save it.

2 Make a new section in your sandbox:

== Task 8 ==

and create a test for your "mum" function. Save it.

3 Amend your code so that it will display "Hello Dad" instead. Check it in your sandbox. (Do the check for each of the following instructions.)

Note that family[i] (where i is a variable containing a number) will refer to the ith member of the "family" table. Arrays in Lua start at 1 by default.

4 Amend your code so that it uses a numeric for loop to go from 1 to 5 to display "Hello " followed by each member of the "family" table.

5 Amend your code so you add two more names to the "family" table. These won't automatically display.

You can find the size of a simple table like "family" by using #family which gives the number of members of the table.

6 Amend your code so that it always displays all of the "family" members.

7 Amend your code so you add one more name to the "family" table. Check that it displays.

8 Leave a comment here linking to your sandbox.

Task tags

  • lua
  • scribunto

Students who completed this task

Gammaburst_3000, C. F. Fairfeld, Chen Xinpeng, Yajn Bollapragada, Sanath, Kid116, OHM OMAR, Tarun Prasad, Jan Nowosielski, suufi, Priyanshu Ahuja, Linda Thelen, Gabriel CH Lee, Gabe Mitnick, Om Desai

Task type

  • code Code
close

2017