Found 82 Articles for Lua

Alternatives to Lua as an Embedded Language

Mukul Latiyan
Updated on 20-Jul-2021 13:50:23

636 Views

An Embedded language is a language that can be used in an application. It is a programming language that adds an ease of performing operations in a specific application.There are many embedded languages that you can use, the most common ones are Lua, LISP, VBA etc.When it comes to choosing the best embedded language, there are several factors that we need to consider. The most basic factor would be to know what are the basic drawbacks that one can have regarding the use case of the language.While Lua does a pretty good job to check or satisfy most of the ... Read More

__tostring element in Lua Programming

Mukul Latiyan
Updated on 20-Jul-2021 13:46:34

2K+ Views

The _tostring element in Lua receives an argument of any type and converts it to a string in a reasonable format.If the metatable of e has a "__tostring" field, then tostring calls the corresponding value with e as argument, and uses the result of the call as its result.The __tostring element method is a part of the metatables that Lua library provides us and is used to modify the behaviour of the table that we get as an output.The __tostring element method is used to modify the behavior of the output table.Example Live Democurrtable = setmetatable({ 10, 20, 30 }, { ... Read More

io.popen() function in Lua Programming

Mukul Latiyan
Updated on 20-Jul-2021 13:43:35

9K+ Views

Sometimes we want to execute the system's command and then make use of whatever was returned by them, and in order to do that we simply can either make use of the os.execute() function or io.popen() function.The difference between the os.execute() function and the io.popen() function is that the output value of the os.execute() function is much harder to deal with, and that is the reason why it is recommended to use the io.popen() function, whose output value is much easier to handle and make use of.io.popen() starts the program in a separate process and returns a file handle that ... Read More

Install Lua in Linux and implement PHP Lua Extension

Mukul Latiyan
Updated on 20-Jul-2021 13:42:01

307 Views

There are cases where we would like to implement PHP Lua extension in PHP.ini so that we can make use of Lua as an embedding language for our PHP code.It can be done with some series of steps that need to be run in a specific manner and the most important of them is running the --with-lua-version command.The steps for installing Lua in Linux are −pecl download lua cd lua-2.0.7In the steps above we made use of the pecl which stands for PHP extension community library to download lua and then we are relocating to the lua directory. Now, it’s ... Read More

Inline conditions in Lua (a == b ? “yes” : “no”)

Mukul Latiyan
Updated on 20-Jul-2021 13:38:25

1K+ Views

You might have noticed ternary operators in different programming languages, but since there’s no ternary operator in Lua, as per the official documentation, we can create one for ourselves with the help of the Lua operators.Let’s first understand what a ternary operator is and why we need one.ExampleConsider the example shown below, which depicts a simple if else condition in lua. Live Demoa = 3 b = 4 if a == b then print("blah") else print("nah nah") endOutputnah nahIn the above if else condition, we wrote multiple lines of code and also used many statements that the lua language provides, but ... Read More

How to work with MySQL in Lua Programming?

Mukul Latiyan
Updated on 20-Jul-2021 13:36:12

1K+ Views

Lua provides different libraries that once can be used to work with MySQL. The most popular framework that enables us to work with MySQL in Lua is LuaSQL.LuaSQL is a simple interface from Lua to a DBMS. It enables a Lua program to −Connect to ODBC, ADO, Oracle, MySQL, SQLite, Firebird and PostgreSQL databases;Execute arbitrary SQL statements;Retrieve results in a row-by-row cursor fashion.You can download MySQL with the help of this command −luarocks install luasql-mysqlMySQL DB SetupIn order to use the following examples to work as expected, we need the initial db setup. The assumptions are listed below.You have installed ... Read More

How to use the Time package in Lua programming?

Mukul Latiyan
Updated on 20-Jul-2021 13:32:40

2K+ Views

Lua library provides us with a time package that can be used to calculate the current time and that current time can be converted into hours, days and minutes and we can also take the later values and turn them into a Lua representation of time.In order to make use of the library time package, we don’t necessarily need to require anything, we just need to write the following command in a Lua script and we are done.Lua code for printing the current time in Lua format −Example Live Demoprint(os.time())Output1624642168The output of the above time command definitely isn’t something that we ... Read More

How to use the require function in Lua programming?

Mukul Latiyan
Updated on 20-Jul-2021 13:31:18

8K+ Views

Lua offers a high-level function which we can use when we want to load and run libraries. This high-level function is named require function.The require function mainly targets the high level functions and keywords.The require function is a bit similar to the dofile function, but it has two key differences, the first one being that it searches for the file in a specified path and the second one is that it mainly focuses on to control whether the file is already running on the script or not.Syntaxrequire “module-name” // some codeHow Does the require Function Work in Lua?It is mainly ... Read More

How to Use the Remove function in Lua programming?

Mukul Latiyan
Updated on 20-Jul-2021 13:29:43

1K+ Views

There are cases when we want to remove an element from a table. In Lua, the table library provides functions to remove elements from the table.The remove function usually takes two arguments, the first argument is usually the name of the table from which we want to remove the element from, and the second argument is the position from which we want to remove the element from.Let’s explore different examples of the remove function.Syntaxtable.remove(x, pos)The x in the above example denotes the name of the table from which we want to remove the element from, and the pos identifier in ... Read More

How to use the Insert function in Lua Programming?

Mukul Latiyan
Updated on 20-Jul-2021 13:27:53

4K+ Views

There are cases when we want to insert elements into a table. In Lua, the table library provides functions to insert elements into the table.The insert function usually takes two arguments, the first argument is usually the name of the table from which we want to insert the element to, and the second argument is the element that we want to insert.If there are three arguments passed to the insert function then the second argument denotes the position in the table where we want to insert the element at.Let’s explore different examples of the insert function.Syntaxinsert(x, element) or insert(x, pos, ... Read More

Advertisements