Found 33676 Articles for Programming

How to search for an item in a Lua List?

Mukul Latiyan
Updated on 20-Jul-2021 13:20:38

4K+ Views

When we want to iterate over an item to find a specific value we usually make use of the for loop. It’s always the most intuitive approach and it is a recommended one.Let’s explore an example where we have a list of fruits stored in Lua, and then we want to check if a particular fruit is present in it or not. For that, the most native and yet efficient approach would be to just iterate over the list elements and compare each element with the element we are looking for. This technique or approach is also known as linear ... Read More

How to remove Lua table entry by its key?

Mukul Latiyan
Updated on 20-Jul-2021 13:17:54

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

How to push a Lua table as an argument?

Mukul Latiyan
Updated on 19-Jul-2021 12:41:01

641 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 make a namespace in Lua programming?

Mukul Latiyan
Updated on 19-Jul-2021 12:37:09

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

How to iterate individual characters in a Lua string?

Mukul Latiyan
Updated on 19-Jul-2021 12:35:32

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

How to Implement a Queue in Lua Programming?

Mukul Latiyan
Updated on 19-Jul-2021 12:32:11

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

How to get the number of entries in a Lua table?

Mukul Latiyan
Updated on 19-Jul-2021 12:28:55

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

How to Encode and Decode JSON and Lua Programming?

Mukul Latiyan
Updated on 19-Jul-2021 12:26:59

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

How to embed Lua codes in Java?

Mukul Latiyan
Updated on 19-Jul-2021 12:24:48

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

string.upper() function in Lua programming

Mukul Latiyan
Updated on 19-Jul-2021 12:23:07

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

Advertisements