User:RexxS/GCI-2019-Task05

Lua Task 5 - Lua libraries
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
Look at mw:Extension:Scribunto/Lua reference manual. 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, : 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  function, supplying a parameter   Please make sure you copy it  and then save your user sandbox. You should see the words you supplied, unchanged.

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

4. In your module sandbox replace the line in the middle of the  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 " ". Read mw:Extension:Scribunto/Lua reference manual.

8. Change the function  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
10. In your module sandbox copy this new function, : 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. Then copy these questions at the end of your user sandbox, answer them and save your user sandbox. Q1. What does  match in a Lua pattern? A1. Q2. What does  match in a Lua pattern? A2.

13. In your user sandbox add a new line that calls the  function, supplying a parameter   Please make sure you copy it  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.