Lua Task 4 - Loops and tables
editPrerequisite: Lua Task 3 - Calculations and tests. This task requires independent learning and is more difficult.
Loops
editLua has several control structures if, while, repeat, for
- this task will focus on the simple numerical for loop.
1. Examine this code:
function p.timestable(frame)
local numb = tonumber( frame.args.numb ) or 2
local out = "Times table<br>"
for i = 1, 10 do
out = out .. i * numb .. "<br>"
end
return out
end
Line 2, local numb = tonumber( frame.args.numb ) or 2
, uses a built-in Lua function called tonumber
which explicitly tries to convert its argument to a number. If it fails, then 2 is used instead as the value for the variable numb. The variable is created as local
, which means that it is destroyed when the program leaves the section where it is defined. The variable numb only exists within the function p.timestable
and not in the rest of the module. This is called its scope. It is a good idea to keep a variable's scope as small as is needed to avoid clashes with other variables using the same name. Variables that are not defined as local are called global and we will generally avoid using them unless absolutely necessary.
Read through the function line-by-line and work out what each line does. The <br>
is a html tag that creates a line-break, i.e. the display continues on the next line.
2. Add a comment -- Task 4
to your module sandbox before the final line return p
.
3. Copy the function p.timestable
into your module sandbox after the comment. Save it.
4. In your user sandbox create a new level 2 heading for this task at the end of the sandbox by typing == Task 4 ==
5. Add a new line that invokes the times function from your module sandbox supplying 4 as the parameter numb
. Use the preview to make sure that you get a four-times table. Save your user sandbox.
6. Edit p.timestable
in your module sandbox so that it displays a heading like 4 times table above the table. Do it in such a way that the heading will always show the number of the times table passed as the parameter. Save it. Check it in your user sandbox by refreshing the page and fix any errors.
7. In your user sandbox add four more test cases for the function, which test how it behaves when given different possible inputs: nothing, blank, text, and a negative number. Use the preview to make sure that the first three show the default times table (2 times) and save your user sandbox. Fix any errors.
8. Edit p.timestable
in your module sandbox so that it will display something like 5 times 1 equals 5, etc. Save it. Check it in your user sandbox by refreshing the page and fix any errors.
9. Edit p.timestable
in your module sandbox so that it displays each table up to 12 times instead of 10 times. Save it. Check it in your user sandbox by refreshing the page and fix any errors.
Tables
editA simple table is demonstrated in the following code:
function p.people(frame)
local friends = {"Agnetha", "Betty", "Carlos", "Davinder", "Eloise"}
local msg = ""
msg = msg .. "Hello " .. friends[2] .. "<br>"
return msg
end
10. Make a copy of the function "p.people" in your module sandbox after the previous function. Use blank lines to keep your functions separate and tidy. Save your module sandbox.
11. Make a new level 3 section at the end of your user sandbox:
=== Tables ===
and create a test for your "people" function in your user sandbox. Once it is working, save the user sandbox.
Make sure you understand why the test displays Hello Betty. The table friends is a simple sequence, i.e. it is indexed by consecutive integers. Unlike other programming languages you may have used, Lua starts its indices at 1, rather than 0.
Note that friends[n] (where n is a variable containing a number) will refer to the nth member of the friends table.
12. Amend the code in your module sandbox so that it will display Hello Agnetha instead, and save it. Check its output in your user sandbox.
13. Amend the code in your module sandbox so that it uses a numeric for
loop around line 4 which loops from 1
to 5
so that the output will be five lines, the first one displaying "Hello Agnetha", the next "Hello Betty", and so on, showing the members of the friends table.
14. Amend the code in your module sandbox so you add two more names to the end of the "friends" table, and save it. Check in your user sandbox that the two new names won't automatically display, because the for
loop only goes from 1 to 5.
You can find the size of a simple table like friends by using #friends
which gives the number of members of the sequence table.
15. Amend the code in your module sandbox so that the upper limit of the for
loop is #friends
. That ensures that it will always display all of the friends members even if the number of them changes. Check in your user sandbox that the two new names now display.
16. Amend the code in your module sandbox to add one more name to the end of the friends table. Check in your user sandbox that it also displays.