Basic Lua

For using the interactive interpreter (REPL), for testing, just start Algernon from the command line, like this:

algernon

Simple values

At the interactive prompt (lua>) try typing 1+1 and you should get 2 back.

Typing nil (representing an empty/non-existing value) should give nil back.

Enclosing text in quotes represents a string, like this: "hello".

Variables can be declared and initialized (be brought into existence and given a value) like this x = 42.

Tables

For initializing a table with three numbers, try:

x = {1, 2, 3}

Just typing x should then return {1, 2, 3}.

For using a table as a dictionary, try:

x = {a=42, b=256, c=123, d=7}

Just typing x should then return {"a":42, "b":256, "c":123, "d":7}. This is a convenience that is built into Algernon. In other Lua interpreters you would usually get either nothing back or something like table: 0x7fecb0c059e0.

For loops

The "proper" way of iterating over a dictionary in Lua is like this:

for key, value in pairs(x) do print(key, value) end

This will output:

d       7
a       42
b       256
c       123

The elements may appear in any order.

The , in print differs from other programming languages in that it separates the fields with a tab character instead of a space. For separating the fields with a single space, the key and value can be combined with a space, like this:

for key, value in pairs(x) do print(key .. " " .. value) end

This will output:

d 7
a 42
b 256
c 123

Again, the elements may appear in any order.

Strings

String concatenation

.. is the operator used in Lua for concatenating strings, or strings and values that can be converted to strings. For this case, the numbers are automatically converted to strings. For manually converting a number to string, the tostring function can be used:

print(tostring(3.141592))

This will output:

3.141592

String representations

Objects in Lua can implement a __tostring metamethod in order to customize how values are converted to strings. A metamethod is a function that has been given special meaning in Lua. Here is an example for how to create a custom string conversion function for a table:

x = {"just", "a", "normal", "conversation"}
metatable = { __tostring = function(self) return "INVASIVE HELLO" end }
setmetatable(x, metatable)
print("Greeting: " .. tostring(x))

This should output: Greeting: INVASIVE HELLO.

In a script file, the variable declarations should be preceeded by local, in order to declare them as local variables (as opposed to global), like this:

local x = 3

This limits the scope of the variable and makes it easier to organize larger programs.

The interactive Lua interpreter

Multiple lines

The interpreter does not support pasting in multiple lines, only one line at the time. For executing several Lua statements, it's possible to sepearate them with a semicolon, like this:

print("hello"); print("there")

For working more comfortably with Lua, it's better to put the code in a Lua file and run it with Algernon.

Listing available functions

There is a built-in help function for giving a quick overview of the available functions specific to Lua and Algernon. There are also general Lua functions that are available for all Lua interpreters.

help

Will output a long list of functions. Shift-PgUp and Shift-PgDn can normally be used for browsing up and down in the terminal output.

webhelp and confighelp are also available.

Exiting

Exit the Lua interpreter by typing exit or pressing ctrl-d two times. If you exit by pressing ctrl-c, you might have to type reset afterwards, depending on the terminal emulator.