Line Filters in Lua
Our line filter program reads input from stdin, processes it, and then prints a derived result to stdout. In this case, it will write a capitalized version of all input text. You can use this pattern to write your own Lua line filters.
In this Lua version:
We don’t need to import a separate module for I/O operations as they are part of the Lua standard library.
Lua’s
io.lines()
function provides an iterator that reads input line by line, similar to the scanner in the original example.We use the
string.upper()
function to convert each line to uppercase.Lua doesn’t have a separate error handling mechanism for I/O operations like this. If there’s an error reading from stdin, it will typically raise an error that will terminate the program.
To try out our line filter, first make a file with a few lowercase lines:
Then use the line filter to get uppercase lines:
This Lua script provides the same functionality as the original example, reading input line by line, converting each line to uppercase, and printing the result.