Troubleshooters.Com and Code Corner Present

Litt's Lua Laboratory:
Lua Hello World
(With Snippets)

Copyright (C) 2011 by Steve Litt



Debug like a Ninja

Here's a Lua hello world:
#!/usr/bin/lua 
print("Hello World")
The first line tells Linux to run this program with /usr/bin/lua. The second line tells it to print "Hello World". Save the program as "hello.lua", use chmod to make it executable, and run it. Here's the program's output:
slitt@mydesk:~$ ./test.lua
Hello World
slitt@mydesk:~$
Now let's add a couple more lines:
#!/usr/bin/lua 
print("Hello World")
print("Hello World yourself")
print("Howdy")
Run it and it looks like this:
slitt@mydesk:~$ ./hello.lua
Hello World
Hello World yourself
Howdy
slitt@mydesk:~$
Just what you'd expect. Now let's explore comments in Lua. There's a "to end of line" comment, and "between these" comment. The "to end of line" comment is a double hyphen, as shown to add a comment to the end of the first line and comment out the second line:

#!/usr/bin/lua
print("Hello World") --This is the first line
-- print("Hello World yourself")
print("Howdy")
Run it and you get just what you expect:
slitt@mydesk:~$ ./hello.lua
Hello World
Howdy
slitt@mydesk:~$
Now let's remove those comments and comment out the first two lines with an inclusive comment:
#!/usr/bin/lua 
--[[print("Hello World")
print("Hello World yourself") ]]
print("Howdy")
As you can see, everything between --[[ and ]] is commented out. That's the way you inclusively comment out. It's often used to comment out large blocks of code. Typically per-line comments are used to actually comment things. Here's the result of the preceding code:
slitt@mydesk:~$ ./test.lua
Howdy
slitt@mydesk:~$
Just as expected. Now you know how to write a simple Lua program.


 [ Troubleshooters.com| Code Corner | Email Steve Litt ]

Copyright (C) 2011 by Steve Litt --Legal