Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 1154 of 3366
7K+ Views
Let’s consider an example where we would like to remove a Lua table entry. The table itself behaves like a hashmap, where it has several key value pairs, and we would like to remove an entry from that table on the basis of the key.Lua library does provide a function that we can use for our specific case. The function is table.remove() and it takes two arguments, the first argument is the name of the table and the second argument is the key that we want to remove.ExampleConsider the example shown below − Live Demolocal mapone = { [1] = 10, ... Read More
652 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
2K+ Views
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
3K+ Views
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
1K+ Views
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
4K+ Views
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
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
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
8K+ Views
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
78K+ Views
Another important function of the Lua’s string library is the string.sub() function. The string.sub() function is used to extract a piece of the string.The string.sub() function takes three arguments in general, the first argument being the name of the string from which we want to extract a piece, the second argument is the i-th index or say, the starting index of the string piece that we want, and the third and the last argument is the j-th index of the last index of the string piece we want.It should be noted that both the starting index and the ending index, ... Read More