User:RexxS/GCI-2018-Task04
Lua Task 4 - Loops and tables
[edit]Prerequisite: Lua Task 3 - Calculations and tests. This task requires independent learning and is more difficult.
Loops
[edit]Lua has several control structures if, while, repeat, for
- this task will focus on the simple numerical for loop.
1. Examine this code:
p.times = function(frame)
local num = tonumber( frame.args.num ) or 2
local out = "Times table<br>"
for i = 1, 10 do
out = out .. i * num .. "<br>"
end
return out
end
Line 2, local num = tonumber(frame.args.num) 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 num. The variable is created as local
, which means that it is destroyed when the program leaves the section where it is defined. The variable num only exists within the function p.times
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.times
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.
5. Add a new line that invokes the times function from your module sandbox supplying 6 as the parameter num
. Use the preview to make sure that you get a six-times table. Save your user sandbox.
6. Edit p.times
in your module sandbox so that the heading it will display in your user sandbox starts with 6 times 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. Make sure that the first three show the default times table (2 times) and save your user sandbox. Fix any errors.
8. Edit p.times
in your module sandbox so that it will display 6 times 1 equals 6, etc. Save it. Check it in your user sandbox by refreshing the page and fix any errors.
9. Edit p.times
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
[edit]A simple table is demonstrated in the following code:
p.mum = function(frame)
local family = {"Dad", "Mum", "Uncle Stan", "Aunty Elsie", "Brian"}
local msg = ""
msg = msg .. "Hello " .. family[2] .. "<br>"
return msg
end
10. Make a copy of the function "mum" 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 "mum" function. Save your user sandbox.
Make sure you understand why the test displays Hello Mum. The table family 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 family[n] (where n is a variable containing a number) will refer to the nth member of the family table.
12. Amend the code in your module sandbox so that it will display Hello Dad 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 Dad", the next "Hello Mum", and so on, showing the members of the family table.
14. Amend the code in your module sandbox so you add two more names to the "family" 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 family by using #family
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 #family
. That ensures that it will always display all of the family 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 family table. Check in your user sandbox that it also displays.