Lua Task 5 - Lua libraries edit

Prerequisite: Lua Task 4 - Loops and tables. This task requires research and independent learning. It may be unsuitable for beginners to programming.

We often have to do the same sort of task regularly, so programming languages usually have available pre-written functions to do these tasks. They will often be collected together by type and referred to as a library. Lua has a set of standard libraries and Scribunto provides some others, especially useful for MediaWiki software.

Lua has a number of built-in libraries, such as the table library and the math library, and you can read about them at Standard Libraries. These supply variants of many of the standard features found in other popular programming languages like PHP and Python, such as regular expressions and string handling functions. Because Lua can access libraries written in C, custom extensions can be implemented.

Sub-strings and case edit

Look at mw:Extension:Scribunto/Lua reference manual #String library. You don't need to learn everything there, you just need to know where to find it.

1. In your module sandbox copy this framework for a new function, p.sent:

function p.sent(frame)
	local txt = frame.args.text or ""
	local out = txt
	return out
end

2. Save your module sandbox.

3. In your user sandbox make a new level 2 heading for this task. Below that add a line that calls the sent function, supplying a parameter |text=this is simple. Please make sure you copy it exactly and then save your user sandbox. You should see the words you supplied, unchanged.

There is a Lua function called "string.sub". Read mw:Extension:Scribunto/Lua reference manual #string.sub.

4. In your module sandbox replace the line in the middle of the p.sent function (line 3 above) with this:

local out = string.sub(txt, 1, 1)

5. Save your module sandbox and observe the result by reloading your user sandbox.

6. Change the line you just altered to this:

local out = string.sub(txt, 2)

7. Save your module sandbox and observe the result by reloading your user sandbox.

There is a Lua function called "string.upper". Read mw:Extension:Scribunto/Lua reference manual #string.upper.

8. Change the function p.sent in your module sandbox so that it takes the first letter of the string supplied and turns it into a capital (this is called "sentence case"). You need to get the first letter and capitalise it, then get the rest of the string, and concatenate the two parts.

9. Save your module sandbox and observe the result by reloading your user sandbox.

Patterns and matching edit

10. In your module sandbox copy this new function, p.unpack:

function p.unpack(frame)
	local dmy = frame.args.dmydate or ""
	local d, m, y = string.match(dmy, "(%d+) (%w+) (%d+)")
	return "Year = " .. y .. "<br>Day = " .. d .. "<br>Month = " .. m
end

11. Save your module sandbox. Note that in line 3, the function returns three values, so we can assign three variables simultaneously.

12. Read mw:Extension:Scribunto/Lua reference manual #Patterns. Then copy these questions at the end of your user sandbox, answer them and save your user sandbox.

Q1. What does %w+ match in a Lua pattern?
A1.

Q2. What does %d+ match in a Lua pattern? 
A2.

13. In your user sandbox add a new line that calls the unpack function, supplying a parameter |dmydate=31 October 2018 Please make sure you copy it exactly and then save your user sandbox. You should see the date unpacked into its year, month and day. Can you see how it works?

14. In a new paragraph in your user sandbox, write the pattern you would use to extract the day, month and year from an American-style date like "October 31, 2018". You don't have to change the code in your module sandbox, just write the pattern in your user sandbox.