Mukul Latiyan

Mukul Latiyan

363 Articles Published

Articles by Mukul Latiyan

Page 34 of 37

How to Compile a Lua Executable?

Mukul Latiyan
Mukul Latiyan
Updated on 20-Jul-2021 807 Views

While there are different ways with which a person can compile a Lua executable, some of them require more time and resources.The most basic approach is to set the Lua Path environment variable and then simply run the lua command. While this might seem to be not much of a pain, setting the Lua Path definitely requires some work.Instead of setting the Lua path, we can simply make use of the env and lua combination which will allow us to convert a Lua file into an executable or run a Lua script.The command shown below does the same −#!/USR/BIN/ENV LUAIn ...

Read More

How to Call a Lua function from C?

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

Calling a Lua function from C is something that requires a series of steps and a mastery in the Lua library functions. Lua provides several library functions that we can use whenever we want to call Lua functions from C or the opposite.Some of the most commonly used Lua library functions for calling a Lua function from C are −luaL_dofile(L, "myFile.lua");lua_getglobal(L, "add");lua_pushnumber(L, a);and much more.We will make use of these functions when we will call a Lua function from C.The first step is to shut down the Lua interpreter and for that we need to write a code in C.ExampleConsider ...

Read More

Command Line arguments in Lua

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

Handling command line arguments in Lua is one of the key features of any programming language. In Lua, the command line arguments are stored in a table named args and we can use the indices to extract any particular command line argument we require.Syntaxlua [options] [script [args]]The options are −-e stat− executes string stat;-l mod− "requires" mod;-i− enters interactive mode after running script;-v− prints version information;--− stops handling options;-− executes stdin as a file and stops handlingoptions.ExampleLet’s consider an example where we will open a Lua shell in interactive mode and we will pass the script as dev/null and then we ...

Read More

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 562 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 708 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
Showing 331–340 of 363 articles
Advertisements