Programming Articles - Page 1152 of 3363

Command Line arguments in Lua

Mukul Latiyan
Updated on 20-Jul-2021 14:00:08

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
Updated on 20-Jul-2021 13:53:46

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

Array Size in Lua Programming

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

5K+ Views

It is a general convention that the size of the array is the number of elements that are present inside the array before the nil. In many cases, nil is not allowed in the array, but for some applications, it is not an issue to have nil inside them.If we allow nil values inside the array, then we must have some functions to set the size of the array explicitly.Lua does provide us with two functions to manipulate the size of the array and these are −setngetnThe setn function is used to set the size of the array explicitly and ... Read More

Alternatives to Lua as an Embedded Language

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

928 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

3K+ 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

17K+ 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

533 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

2K+ 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

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 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

Advertisements