Programming Articles - Page 1153 of 3363

How to use the require function in Lua programming?

Mukul Latiyan
Updated on 20-Jul-2021 13:31:18

14K+ Views

Lua offers a high-level function which we can use when we want to load and run libraries. This high-level function is named require function.The require function mainly targets the high level functions and keywords.The require function is a bit similar to the dofile function, but it has two key differences, the first one being that it searches for the file in a specified path and the second one is that it mainly focuses on to control whether the file is already running on the script or not.Syntaxrequire “module-name” // some codeHow Does the require Function Work in Lua?It is mainly ... Read More

How to Use the Remove function in Lua programming?

Mukul Latiyan
Updated on 20-Jul-2021 13:29:43

2K+ Views

There are cases when we want to remove an element from a table. In Lua, the table library provides functions to remove elements from the table.The remove function usually takes two arguments, the first argument is usually the name of the table from which we want to remove the element from, and the second argument is the position from which we want to remove the element from.Let’s explore different examples of the remove function.Syntaxtable.remove(x, pos)The x in the above example denotes the name of the table from which we want to remove the element from, and the pos identifier in ... Read More

How to use the Insert function in Lua Programming?

Mukul Latiyan
Updated on 20-Jul-2021 13:27:53

5K+ Views

There are cases when we want to insert elements into a table. In Lua, the table library provides functions to insert elements into the table.The insert function usually takes two arguments, the first argument is usually the name of the table from which we want to insert the element to, and the second argument is the element that we want to insert.If there are three arguments passed to the insert function then the second argument denotes the position in the table where we want to insert the element at.Let’s explore different examples of the insert function.Syntaxinsert(x, element) or insert(x, pos, ... Read More

How to Use lua-mongo library in Lua Programming?

Mukul Latiyan
Updated on 20-Jul-2021 13:26:10

1K+ Views

Lua provides different libraries that can be used to work with MongoDB. The most popular framework that enables us to work with MongoDB in Lua is lua-mongo.Lua-mongo is a binding to MongoDB C Driver for Lua −It provides a Unified API for MongoDB commands, CRUD operations and GridFS in MongoDB C Driver.Transparent conversion from Lua/JSON to BSON for convenience.Automatic conversion of Lua numbers to/from BSON Int32, Int64 and Double types depending on their capacity without precision loss (when Lua allows it). Manual conversion is also available.You can download MongoDB with the help of this command −luarocks install lua-mongoMongoDB SetupIn order ... Read More

How to use Lua Programming to check if a file exists?

Mukul Latiyan
Updated on 20-Jul-2021 13:24:28

3K+ Views

Lua provides us with different functions and methods that we can use when we want to work with files. These methods or functions do different operations, like from opening a file, to closing a file, to opening a file with a specific mode also.While many of these functions that Lua provides to work with files, two of them are more subtle to use and work with.In this article we will explore these two approaches, where in the first approach we will simply open a file by passing the name of the file and the mode in which we want to ... Read More

How to split a string in Lua programming?

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

26K+ Views

Splitting a string is the process in which we pass a regular expression or pattern with which one can split a given string into different parts.In Lua, there is no split function that is present inside the standard library but we can make use of other functions to do the work that normally a split function would do.A very simple example of a split function in Lua is to make use of the gmatch() function and then pass a pattern which we want to separate the string upon.ExampleConsider the example shown below − Live Demolocal example = "lua is great" for ... Read More

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

674 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

Advertisements