A Look at Lua
Because any value can represent a condition, booleans in Lua differ from those in many other languages. Both false and nil are considered false in Lua, but Lua considers everything else true (including zero and an empty string).
Unlike Python, global variables do not need to be declared. To create one, assign a value to it. To delete it, give it the nil value. A global variable exists only if it has a non-nil value. Exactly the opposite of Python, most variables in Lua are global by default, and you must declare the variable “local” to make it a local variable rather than assuming that all variables are local.
Because most CPUs perform floating-point arithmetic just as fast as integer arithmetic, numbers in Lua represent real, double-precision, floating-point numbers rather than common integers. Because Lua doesn't need integer types, it doesn't have them. This eliminates rounding errors, floating-point numbers and long integers.
Lua handles strings very adeptly and has been used for strings that are several megabytes long. It converts between strings and numbers; any numeric operation applied to a string converts the string to a number. This conversion principle applies only to numbers, as Lua converts numbers to strings when strings are expected. Even with automatic conversion, Lua still can differentiate between numbers and strings in cases like 90 == “90” (which always is false).
Identifiers starting with an underscore (such as _FOO) are not recommended for use in Lua, because many are reserved for special uses. As long as an identifier does not begin with a digit, the identifier can be made up of a combination of underscores, letters and digits.
You can basically rename anything in Lua, even to the point of making it un-callable. Take, for example, the following code:
x = io x.read() io = "Hello world!" x = "Let's make io uncallable!" io.read()
The second line gets keyboard input through the io module. Because io is essentially a variable with a function as a value, you can give it a different value so that io does not relate to the input/output functions anymore. When you try to get keyboard input from the io module again, Lua returns an error. The program is unable to call the input/output functions now that io's value has been reassigned. In order to use io again, you must restart the Lua program.
Lua concatenates strings with the .. operator. Note that print("Hello".."World!") is valid, but print("I've said 'Hello World'"..5.."or more times.") is not. This is because Lua sees the periods as decimals after the integer. The operator must have a space between the strings and the integer. Otherwise, it won't return an error. The following code validly concatenates the strings and the integer:
print("I've said 'Hello World' " ..5 .. " or more times.")
Lua uses many of the common operators that Python, Ruby and most every other language use. For the Python/Ruby logical not operator, Lua can either use it or use ~= for the negation of equality. Always remember that Lua treats strings and integers differently: "1" < 2 is always false, and strings are compared alphabetically.
Lua ends while loops if the condition is false. Repeat-until statements are the opposite of while loops; they loop until the condition is true. for loops have some hidden twists, which can be annoying to Python or Ruby programmers. Local variables created in the for loop are visible only inside the loop. The variable does not exist when the loop ends, so if you need the value of the control variable, you have to save its value into another loop. Breaks or returns should appear only as the last statement before an end, an else or an until in a loop for syntactic reasons.
Lua treats functions as “first class” values and uses them for OOP (object-oriented programming). Lua can call its own functions or C functions, and it handles functions as a type. You can give a variable the function property or create it with the function() method. Functions written in Lua can return multiple results if you list them after a return keyword.
Lua supports OOP, but due to Lua's size, its implementation of OOP lacks a few features. Lua uses tables and functions for OOP rather than classes. In the same way that Python accesses a function or variable in a class, Lua accesses it with Table.function or Table.variable.
Lua can be picky when it comes to multiple assignment, because it adjusts the number of values on the assignment. If the amount of values is less than the list of variables, all remaining values are given the nil value. If the list of values is longer than the amount of variables, Lua silently discards them.
|Alice, the Turtle of the Modern Age||Mar 07, 2014|
|Using Django and MongoDB to Build a Blog||Mar 05, 2014|
|What virtualization solution do you use/manage at work?||Mar 04, 2014|
|Our Assignment||Mar 04, 2014|
|March 2014 Issue of Linux Journal: 20 Years of Linux Journal||Mar 03, 2014|
|Have Resume - Will Travel||Feb 28, 2014|
- Alice, the Turtle of the Modern Age
- Using Django and MongoDB to Build a Blog
- Linux Systems Administrator
- Sign Up to Win a Silicon Mechanics Swag Pack!
- Senior Perl Developer
- Technical Support Rep
- Our Assignment
- UX Designer
- Zato—Agile ESB, SOA, REST and Cloud Integrations in Python
- March 2014 Issue of Linux Journal: 20 Years of Linux Journal
- You have to be careful there
1 week 4 days ago
- Wonder when LJ is going to
1 week 4 days ago
- Puerto Rico Free Software
1 week 5 days ago
1 week 6 days ago
- I hate voice commands
2 weeks 9 hours ago
- usabilty --- AGAIN with this nonsense
2 weeks 10 hours ago
- Don't make excuses
2 weeks 14 hours ago
- Sorry to let you know, but
2 weeks 14 hours ago
- Ridiculous statement. Not a
2 weeks 1 day ago
2 weeks 1 day ago