Programming

Lua


Lua is a fast, powerful, lightweight, embeddable scripting language. It was designed around being portable and easy to use. Lua is used around the world in various applications, including CGI webpages, powerful configuration files, and a language for creating extensions to other programs, especially games, such as Roblox, Garry’s Mod, and the ComputerCraft mod for Minecraft.

Download software: The Lua programming language interpreter. (Download source for compilation from http://www.lua.org/download.html OR download installer package for Windows from https://code.google.com/p/luaforwindows/)

Cost: Free (Licensed under the MIT License)

Instructions to setup software: Compile source code via the makefile or run installer package for Windows.

Hardware requirements: Any platform that has an ISO/ANSI C compiler, some others.

Operating systems supported: Any that are an executable target from any ISO/ANSI C compiler, some others.

Difficulty level: 2 out of 5


TRY IT!

1. Create a folder on your hard drive – call it “Lua Demo”.

2. Copy this Lua code and paste into your favorite text editor. Windows notepad works fine.


-- A Lua program.
-- Double hyphens indicate comments, which aren't actually run as code and are notes for other programmers.

--[[
  if you use the double hyphens and then double square brackets,
  you can have comments that span multiple lines,
  as long as you end it with closing brackets!
]]

-- Write a greeting onto the screen
io.write("Hi there! What's your name? ")

-- Create a global variable called 'name', giving it a value assigned from keyboard input
name = io.read()

--[[
  Write a message on the screen, using the value within name.
  The double dots (..) tell Lua to concatenate (or combine) the string
  with the value from name. The "\n" tells Lua to start a new line
]]

io.write("Hello, "..name.."! I'm your computer!\n")
io.write("Let's do some math.\nYou give me two numbers, and I'll add them up.\n")

-- Read in a value into number1
io.write("What's the first number? ")
number1 = io.read()

--[[
  Check that number1 really is a number. The tonumber() function will return a value
  that will cause the logical while statement to succeed and continue if the variable is a number.
  Otherwise, continue to ask for a new value until it is a number.
]]

while not tonumber(number1) do
  io.write("That's not a number!\n")
  io.write("What's the first number? ")
  number1 = io.read()
end

-- Do the exact same thing with a new variable called number2.

io.write("What's the second number? ")
number2 = io.read()

while not tonumber(number2) do
  io.write("That's not a number!\n")
  io.write("What's the second number? ")
  number2 = io.read()
end

-- Add up the numbers
answer = number1 + number2

-- Display the final result and a good-bye
io.write(number1.." + "..number2.." = "..answer.." !\n")
io.write("It was great talking to you, "..name.."!\n")
io.write("See you later!\n")

3. Save the File as “Lua-Example.lua” in the new folder you created in step 1.

4. Double click on the Lua file to launch a Lua interpreter, if you installed for Lua for Windows package. Otherwise, use the command line to run Lua. (Enter ‘lua Lua-Example.lua’ in the command line)

Enter you name and a couple numbers and see what happens! Try several different names and numbers.


TRY THIS

1. Change the math operation that the computer uses for the numbers.

2. Change the wording of the phrases.

3. Add another text input –- ask for a third number.


LEARN MORE

Reference Manual for Lua 5.2: A full manual for the Lua language.

Programming in Lua: A book written by one of the Lua language designers that covers many different topics about Lua.

www.lua-users.org: A user-created wiki about Lua containing many tutorials and other documents.


RESOURCES, TIPS, TRICKS AND HINTS

Notepad++ is a great free text editor designed specifically for programmers. Check it out here.

Comments about “Lua”

  1. jj says:

    i dont get it

Write a comment about “Lua”

Nickname:

Type your comment:

  • 1. Safety. Do the following:
      a. Show your counselor your current, up-to-date Cyber Chip.
      b. Discuss first aid and prevention for the types of injuries or illnesses that could occur during programming activities, including repetitive stress injuries and eyestrain.

    2. History. Do the following:
      a. Give a brief history of programming, including at least three milestones related to the advancement or development of programming.
      b. Describe the evolution of programming methods and how they have improved over time.

    3. General knowledge. Do the following:
      a. Create a list of 10 popular programming languages in use today and describe which industry or industries they are primarily used in and why.
      b. Describe three different programmed devices you rely on every day.

    4. Intellectual property. Do the following:
      a. Explain how software patents and copyrights protect a programmer.
      b. Describe the difference between licensing and owning software.
      c. Describe the differences between freeware, open source, and commercial software, and why it is important to respect the terms of use of each.

    5. Projects. Do the following:
      a. With your counselor’s approval, choose a sample program. Then, as a minimum, modify the code or add a function or subprogram to it. Debug and demonstrate the modified program to your counselor.
      b. With your counselor’s approval, choose a second programming language and development environment, different from those used for requirement 5a and in a different industry from 5a. Then write, debug, and demonstrate a functioning program to your counselor, using that language and environment.
      c. With your counselor’s approval, choose a third programming language and development environment, different from those used for requirements 5a and 5b and in a different industry from 5a or 5b. Then write, debug, and demonstrate a functioning program to your counselor, using that language and environment.
      d. Explain how the programs you wrote for requirements 5a, 5b, and 5c process inputs, how they make decisions based on those inputs, and how they provide outputs based on the decision making.

    6. Careers. Find out about three career opportunities in programming. Pick one and find out the education, training, and experience required. Discuss this with your counselor and explain why this career might be of interest to you.

  • Earning the Cyber Chip can help you learn how to stay safe while you are online and using social networks or the latest electronic gadgets.

    Topics include cell phone use, texting, blogging, gaming, cyberbullying, and identity theft. Find out more about the Cyber Chip at www.scouting.org/cyberchip.