Lua math & io Libraries
03/16/21 08:33 Filed in: Lua
I'm going to divert a bit and dive into the math Lua library and its functions. I want to see what is out there. I also want to learn how to work with files.
I don't like reinventing the wheel (because I tend to). Libraries and their functions keep your from doing just that. I want to go through some of the libraries to give me an idea of their capabilities, and more importantly, teach me about the wheels of the language. I'm just going to list a lot of things I find interesting, or useful in this post. It won't be exhaustive and I won't include explanations, unless they are needed. I also want to find out how to work with files.
Math
Apart from the basic math operators, such as addition, it's useful to know how to do common things with numbers. Lua has a math library which contains a lot of functions that you would expect (such as max and min). There are a lot of trigonometry and conversion functions:
- math.acos(v)
- math.sin(v)
- math.tan(v)
- etc.
Lua allows you to convert numbers:
- math.deg(v) - from radians to degrees
- math.rad(v) - from degrees to radians
- math.tointeger(v)
- etc.
Random number are always useful:
- math.random(min, max)
- math.random(max) - between 1 and max
- math.random() - between 0 and 1
Numeric limits:
- math.huge - positive infinity
- math.mininteger - smallest integer
- math.maxinteger - largest integer
Files
I read and write a lot of text files. Knowing how to do this is always important. Lua has an io library.
- Io.open(file) - opens a file in read-only mode and returns a handle to it.
- io.open(file, mode) - if you specify a mode you can alter the file for other than read-only mode. The modes are:
- "r" - read-only (the default)
- "w" - write mode. This will open a new file for writing. If it already exists, it will be destroyed (overwritten).
- "a" - append - Opens a file and creates a new one if it it doesn't exist.
- "r+" - read-write for an existing file. The existing contents are destroyed. No new file is created (returns nil if the file doesn't exist.)
- "w+" - same as "w", but allows reading.
- "a+" - same as "a", but allows reading.
Let me try opening a text file.
Well, that's exciting. Actually, it is. I can open a file and get a file handle back.
Writing to the file uses the write method of the file handle (think of the file handle as an object).
handle:write(string)
Let's write a file.
That worked! The interesting thing to note here is the write method doesn't include a return character, so my three writes created a single line. Now that I've written a file, let's read it. To do that I can either do so by "chunk" or line by line. Reading line by line is the easiest.
handle:lines()
Returns an iterator you can use to loop through each line in the file. I need a file to read.
And a program to read it.
Here's the output.
Lua also allows you to control what you read and how much. To do this, you use the read method and pass in a parameter that specifies what to read.
- handle:read("*l") - reads a line and returns it, or returns nil if at the end of file.
- handle:read(x) - where x is an integer. Reads that number of characters and returns them. Nil is returned at the EOF. You can test for EOF by setting x to 0. Note, there is no asterisk in this parameter
- handle:read("*a") - reads all of the file, and an empty string at EOF
- handle:read("*n") - reads and returns a number, not a string.
Let's read the same file.
Finally, to close things out (pun intended), I need to know how to close the file once I'm done with it. That's as simple as calling:
handle:close()
In the previous examples, I've left the file open. That's a bad thing, but I'm only testing. Always close your files.
That's enough for today. I'll look at a few more libraries next time.