A regular expression is a special text string that is used to describe a search pattern.PCRE (Perl Compatible Regular Expressions) is a C library implementing regex. It was written in 1997 when Perl was the de-facto choice for complex text processing tasks. The syntax for patterns used in PCRE closely resembles Perl. If you want to learn about PERL and its use cases, you should visit this link.Now, let's take an example to see how to convert a PCRE into Lua and then print it.ExampleConsider the code shown below −"\002\003\004\005\006\007\008\009\010\011\012\”The above string acts as a PCRE and we will convert ... Read More
When working with JSON we generally need to decode JSON into a string or maybe encode a string into JSON. Both of these processes of converting the string into a JSON or the opposite are seen frequently.While Lua doesn’t provide an official library to do the same, we can still make use of the third party libraries.There are many third party libraries that we can make use of, out of these the most common one is the json-lua library which can be located on this link.We can either clone the repository on our local machine and then install it or ... Read More
We know that Lua does a great job when it comes to being a simple language and an embedded language. Lua works even better with C, because of the different libraries that provide a great amount of support for it.In order to compile embedded Lua in C, we need to first write a Lua program followed by a C program that will invoke the Lua program function, and then we will compile the C program.Consider the program shown below as the Lua program −print("--I am using Lua from within C--")It should be noted that the above Lua script should be ... Read More
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
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
The .(dot) operator in Lua is used to invoke the method of an object, and it is a widely used operator in Lua.The :(colon) operator in Lua is used when you want to pass an invisible parameter to the method of an object that you are calling.Let’s consider an example where we will have an object in which two functions are present and we will try to access these functions, first by making use of the dot operator and secondly by making use of the colon operator.ExampleConsider the example shown below − Live DemoreturnX = {foo = function(x, y) return x ... Read More
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
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
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
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
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP