Lua on openSUSE: My experience #1

By | December 20, 2013

This post now details my work with Lua on the preferred operating system, Linux. I use openSUSE 13.1.

Another reason why it’s easier: Lua is already installed. I’m going to open a terminal and get Lua working in my GNOME terminal.

But this time, I’m going to fiddle around with Lua on my interpreter in Linux openSUSE 13.1 then discuss it here as I go along. The goal is to learn Lua by doing stuff with it, not by learning a body of knowledge and then doing stuff with it.

Here are some lines of code I just typed in my interpreter:

print (99 and 100)
print (99 or 100)
print (nil and 33)
print (nil or 33)
print (false and 33)
print (true and 33)
print (false or 33)
print (true or 33)

As you can tell, when you type print (x and y), then it will usually print y. But with print (x or y) it will usually print x.

The exceptions are nil and false. If you type print (nil or x), then it prints x. Otherwise, if you type (nil and x), it prints nil. It doesn’t matter what the order is here.

Now, another set of code I notice:

print (not nil)
print (not 33)
print (not not false)

The word not, as an operator, returns true or false. Generally, not and either “false” or nil generates true in your interpreter, while not and a number generates false.

We justify what we see in our interpreter by the principles of negation here. We define numbers and strings (when defined) to be true, along with the word “true”. Nil, meaning an unassigned value, and false, is associated with “false”.

With regards to concatenation operators, I typed in the following lines of code as an experiment:

print("Golden" .. "State")
print("Golden " .. "State")
Golden State
print(33 .. 44)

I learned that if I type print(“Golden” .. “State”) it would print out two words without spacing. So to solve this problem, I had to put a space between the first word and the quotation mark, then leave space between the quotation mark and the two dots (representing concatenation). Otherwise, the result would be parallel to the first and third line of code I typed in.

The next thing we need to look at is the establishment of tables.

Let’s look at a simple example. I want to construct a “table” in Lua that allows me to store and generate values on the Lua interpreter, with regards to months in a year.

months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}

Now, let’s enter the following.


Then you get January in your interpreter.

At this point, I need to spend some time exploring the table construction feature of Lua. I shall address tables in a later post, so I can discuss how you, the reader (and viewer) can have fun building tables in Lua.

Leave a Reply

Your email address will not be published. Required fields are marked *