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
A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it.In simple words, a namespace is a class of elements in which each element has a unique name to that class. It is used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.In Lua, there’s no such thing as a namespace. Despite not providing support for the same, the official documentation mentions that, “It is sometimes nice to organize your code into packages and modules with ... Read More
A string in Lua is a sequence of characters that we can iterate over in different ways.After Lua 5.1, we have many approaches which we can use to iterate over the characters that made a string, and we can even do anything we want with them, like make use of them in another example, or simply print them.Let’s consider the first and the most basic approach of printing the individual characters of a string.ExampleConsider the example shown below − Live Demostr = "tutorialspoint" for i = 1, #str do local c = str:sub(i, i) print(c) endIn the above example, ... Read More
There are different approaches one can take to declare a queue in Lua; the most basic one is to make use of the tables and repeat function, and take two variables that will help us to insert and delete elements from the queue.ExampleConsider the code shown below − Live Demoqueue = {} queue.first = 0 queue.last = -1 queue.data = {} function insert(q, val) q.last = q.last + 1 q.data[q.last] = val end function remove(q) if (q.first > q.last) then rval = -1 else print("remove: q.data[q.first]= ", q.data[q.first], " q.first= ", ... Read More
While there are many functions and operators that we can use to get the size of the integer entries in a particular table in lua, there's no defined method or function which works for a table that contains data that is not homogeneous in nature.Let’s consider a simple table where we have integer values stored in it, then we will use two most common approaches to find the number of entries in that table.ExampleConsider the example shown below − Live Demot = {} t[1] = 1 t[2] = 2 t[3] = 3 t[4] = 4 print(#t)Output4But let’s take the case where ... Read More
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
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
There are certain scenarios in our code that when we are working with the strings, we might want some string to be in uppercase, like consider a very basic and yet famous example of such scenario, the PAN number.Imagine that you are making a web form in which there’s a field for the PAN number of the user, and since you know that PAN number can’t be in lowercases, we need to take the user input of that field and convert the string into its uppercase.Converting a string to its uppercase in Lua is done by the string.upper() function.Syntaxstring.upper(s)In the ... Read More
There are certain scenarios in our code that when we are working with the strings, we might want some string to be in lowercase, like consider a very basic case of an API which is using an authentication service, and the password for that authentication service should in lowercase, and in that case, we need to convert whatever the password the user enters into lowercase.Converting a string to its lowercase in Lua is done by the string.lower() function.Syntaxstring.lower(s)In the above syntax, the identifier s denotes the string which we are trying to convert into its lowercase.ExampleLet’s consider a very simple ... Read More
There are scenarios when we want to change a pattern that we found in a string with our pattern, and in Lua for that we have a famous library function, named the string.gsub() function.The string.gsub() function has three arguments, the first is the subject string, in which we are trying to replace a substring to another substring, the second argument is the pattern that we want to replace in the given string, and the third argument is the string from which we want to replace the pattern.Syntaxstring.gsub(x, a, b)In the above syntax, the x identifier is used to denote the ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP