print("Hello World")If you want to print a blank line, just use the function without arguments:
print()The print() statement always adds a newline, so each print() statement's contents occur on their own lines. Now let's say you need to write something without a newline, use io.write():
local pi = 3.14159It's often easier to use string.format():
local pi_name = "PI"
io.write("The value of ")
io.write(pi_name)
io.write(" is ")
io.write(pi)
print(", isn't that cool?")
print(string.format("The value of %s is %d, isn't that cool?",pi_name, pi))One problem with string.format() is it's a lot of typing and makes for long lines. So what you can do give it a substitute name at the top of the program:
sf=string.formatYou read from standard input (stdin) via the io.read() function, as shown in the following program that prompts the user to input their name, and then repeats the name:
print(sf("The value of %s is %d, isn't that cool?",pi_name, pi))
#!/usr/bin/luaThe preceding code produces the following output (the bold type is input by the user):
sf=string.format
local answer
io.write("What\'s your name?=>")
answer = io.read()
print(sf("OK, your name is %s.",answer))
slitt@mydesk:~$ ./test.lua
What's your name?=>Steve Litt
OK, your name is Steve Litt.
slitt@mydesk:~$
io.stderr:write("Hello World")NOTICE THE COLON!!! The io.stderr object is a table containing a function called write, so you must either introduce io.stderr as the first argument to its write function, or else use the colon, as will be explained in the article on functions.
#!/usr/bin/luaThe preceding code produces the following output:
print("This should go to stdout")
io.stderr:write("This should go to stderr\n")
print("Again stdout")
io.stderr:write("Again stderr\n")
print("Third stdout")
io.stderr:write("Third stderr\n")
slitt@mydesk:~$ ./test.luaBut now look what happens when you redirect stdin to /dev/null:
This should go to stdout
This should go to stderr
Again stdout
Again stderr
Third stdout
Third stderr
slitt@mydesk:~$
slitt@mydesk:~$ ./test.lua > /dev/nullAnd here's what happens if you send stderr to /dev/null:
This should go to stderr
Again stderr
Third stderr
slitt@mydesk:~$
slitt@mydesk:~$ ./test.lua 2> /dev/null
This should go to stdout
Again stdout
Third stdout
slitt@mydesk:~$
myinputhandle = io.open("myinputfilename", "r")You must test for proper opening. The easiest way is to use assert, like this:
myoutputhandle = io.open("myoutputfilename", "w")
local inf = assert(io.open("brighthouse.log", "r"), "Failed to open input file")If you want a more definitive message or error handling, you can test for the handle being nil and if so do your error routine. I'm a big fan of assert because it's quick and easy while writing code. My opinion is you can always make it more sophisticated later.
local ouf = assert(io.open("junk251.jnk", "w"), "Failed to open output file"
local inf = assert(io.open("brighthouse.log", "r"), "Failed to open input file")Personally I'm not a fan. At least in most cases. My experiments indicate that doing a whole file read into a string and then out again into a file is roughly half the speed of a Linux cp command on the same computer, so on large files it's worth shelling out to Linux and doing the cp command.
local bigstring = inf:read("*all")
local inf = assert(io.open("brighthouse.log", "r"), "Failed to open input file")But the coolest way to read line by line is the io.lines() iterator:
local line = inf:read("*line")
#!/usr/bin/luaFor those occasional fixed length sequential files you can use a number argument for io.read(), as shown:
sf=string.format
local lines = 0
local menus = 0
for line in io.lines("/d/bats/s.emdl") do
lines = lines + 1
if string.match(line, ":::") then menus = menus + 1 end
end
print(sf("The file had %d menus out of %d lines.", menus, lines))
local inf = assert(io.open("/d/bats/s.emdl", "r"))s.emdl isn't fixed length sequential, but I haven't had a fixed length sequential file in years, so I just used my EMDL file. Anyway, here's the resulting output:
local str = inf:read(53)
print(str)
print(#str)
slitt@mydesk:~$ ./test.luaAnother place where numeric arguments to io.read() could be very handy is on files where a couple bytes give you the length of the next section, on and on. PDF files can be kind of like that.
S:::Main Menu
Internet ::: Internet menu
IPCop
par
53
slitt@mydesk:~$
#!/usr/bin/luaIt's easy.
local ouf = assert(io.open("junk251.jnk", "w"))
for i=1, 20 do
ouf:write(tostring(1000 + i))
ouf:write("\n")
end
io.close(ouf)
[ Troubleshooters.com| Code Corner | Email Steve Litt ]
Copyright
(C) 2011 by Steve Litt --Legal