Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Lua basic syntax


May 12, 2021 Lua


Table of contents


Lua basic syntax

Lua is easy to learn and we can create our first Lua program!


The first Lua program

Interactive programming

Lua provides interactive programming modes. We can enter the program on the command line and see the effect immediately.

Lua interactive programming mode can be enabled by commanding lua -i or lua:

$ lua -i 
$ Lua 5.3.0  Copyright (C) 1994-2015 Lua.org, PUC-Rio
> 

On the command line, enter the following command:

> print("Hello World!")

Then we press the enter button and the output is as follows:

> print("Hello World!")
Hello World!
> 

Scripted programming

We can keep the Lua program code to a file that ends in lua and execute it, a pattern called scripting, as we store the following code in a script file called hello.lua:

print("Hello World!")
print("www.w3cschool.cn")

Use the lua name to execute the above script, and the output is:

$ lua test.lua
Hello World!
www.w3cschool.cn

We can also modify the code to execute the script as follows (add at the beginning: s!/usr/local/bin/lua):

#!/usr/local/bin/lua

print("Hello World!")
print("www.w3cschool.cn")

In the above code, we specify Lua's interpreter /usr/local/bin directory. I t is ignored by the mark interpreter with the hashtag. Next we add executable permissions to the script and execute:

./test.lua 
Hello World!
www.w3cschool.cn

Comments

A one-line comment

The two minus signs are one-line comments:

--

Multiple lines of comments

--[[
 多行注释
 多行注释
 --]]

Marker

Lua markers are used to define a variable, and functions get items defined by other users. T he marker begins with a letter A through Z or a to z or an underscore . . . with 0 or more letters, an underscore, and a number (0 to 9).

It's best not to use underscores to increase the marker of letters, as is the case with Lua's reserved words.

Lua does not allow special characters such as s, $, and % to define markers. L ua is a case-sensitive programming language. S o W3c and w3c are two different markers in Lua. Here are some of the correct markers:

mohd         zara      abc     move_name    a_123
myname50     _temp     j       a23b9        retVal

Keywords

Lua's retention keywords are listed below. Reserved keywords cannot be used as constants or variables or other user-defined markers:

and break do else
elseif end false for
function if in local
nil not or repeat
return then true until
while

As a general rule, names that connect a string of capital letters, such as _VERSION, at the beginning of the following dash are reserved for Lua's internal global variables.


Global variable

By default, variables are always considered global.

The global variable does not need to be declared, and once a variable is assigned a value, the global variable is created, and there is no error in accessing a global variable that is not initialized, except that the result is: nil.

> print(b)
nil
> b=10
> print(b)
10
> 

If you want to delete a global variable, you only need to assign the variable to nil.

b = nil
print(b)      --> nil

This variable b is as if it had never been used. In other words, a variable exists when and only if it is not equal to nil.