Lua Articles

Page 5 of 6

Code indentation in Lua Programming

Mukul Latiyan
Mukul Latiyan
Updated on 20-Jul-2021 4K+ Views

Lua codes are not like Python when it comes to indentation. So, most of the code you will write will work even if it falls on another line, and you don’t necessarily need to have the nested code to be intended by a certain tab size.The code indentation in lua is more about making the code look much better and more readable. If your entire code is on one line or worse, if it is like multiple lines then I am afraid that your code isn’t very readable.While we can use the code editor’s indentation packages to do the indentation ...

Read More

io.popen() function in Lua Programming

Mukul Latiyan
Mukul Latiyan
Updated on 20-Jul-2021 18K+ 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
Mukul Latiyan
Updated on 20-Jul-2021 563 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

How to work with MySQL in Lua Programming?

Mukul Latiyan
Mukul Latiyan
Updated on 20-Jul-2021 2K+ 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 lua-mongo library in Lua Programming?

Mukul Latiyan
Mukul Latiyan
Updated on 20-Jul-2021 1K+ Views

Lua provides different libraries that can be used to work with MongoDB. The most popular framework that enables us to work with MongoDB in Lua is lua-mongo.Lua-mongo is a binding to MongoDB C Driver for Lua −It provides a Unified API for MongoDB commands, CRUD operations and GridFS in MongoDB C Driver.Transparent conversion from Lua/JSON to BSON for convenience.Automatic conversion of Lua numbers to/from BSON Int32, Int64 and Double types depending on their capacity without precision loss (when Lua allows it). Manual conversion is also available.You can download MongoDB with the help of this command −luarocks install lua-mongoMongoDB SetupIn order ...

Read More

How to push a Lua table as an argument?

Mukul Latiyan
Mukul Latiyan
Updated on 19-Jul-2021 712 Views

We may want to push the Lua table as an argument to a code written in C++ which uses Lua as an embedded language and for that case we need to make use of the different API functions that the Lua library provides us with.ExampleThe Lua code will look something like the code shown below −a = {    numb = 10,    create = function(a)       print(a);    end, increment = function(self)    --self.numb = 11;    print(self.numb); end, decrement = function(self, i)    self.numb = self.numb-i;    print(self.numb); end }; b = a;And the C++ code ...

Read More

How to Encode and Decode JSON and Lua Programming?

Mukul Latiyan
Mukul Latiyan
Updated on 19-Jul-2021 4K+ Views

JSON is short for JavaScript Object Notation. It is a type of format that is very widely used in the programming world, but it is only a text format in its entirety. There are many JSON libraries available for Lua, but the most commonly used library is lunajson.In this article, we will learn how to install lunajson first with the help of luarocks and then we will see how to work with luna-json and use it to cover the most common cases of encoding and decoding a string to JSON or vice versa. Finally, we’ll go over some more applicable ...

Read More

How to embed Lua codes in Java?

Mukul Latiyan
Mukul Latiyan
Updated on 19-Jul-2021 2K+ Views

Lua is probably the most commonly used embedding language that can be integrated or embedded into different major programming languages. There are different projects that does this work of embedding for us, Lua has been embedded into C, C# and Java also.In this article, we will explore how the embedding of Lua in Java works, and we will also explore the most commonly used project that does this for us.Embedding Lua with Java simply means the fact we should be able to run the code inside the Lua file with the help of the java commands. Just like how we ...

Read More

Read-only tables in Lua programming

Mukul Latiyan
Mukul Latiyan
Updated on 19-Jul-2021 978 Views

While working with tables, we can easily access and modify the values present in the tables provided we know the key. But there are cases where we would like our tables to be in read-only format so that the values present inside the table cannot be modified.There are many benefits for this particular approach as we can make use of such read only tables in storing information that we don’t want anyone to mutate, like storing data of employees.In order to make any table a read only table we make use of the setmetatable() function along with __index and __newindex ...

Read More

Passing Lua script from C++ to Lua

Mukul Latiyan
Mukul Latiyan
Updated on 19-Jul-2021 657 Views

The idea of passing Lua script from C++ to Lua includes the fact that we will have to load the libraries and header files as Lua is ANSI C, and if we are coding in C++, we will need to enclose the #includes in extern “C”.The old and mostly used approach would be to load the libraries that Lua provides and then simply call the C++ function from Lua.In order to load the script from C++ to Lua, we need to set up and shutdown the Lua interpreter and we can do that with the help of the following code.ExampleConsider ...

Read More
Showing 41–50 of 52 articles
Advertisements