Found 82 Articles for Lua

How to Use lua-mongo library in Lua Programming?

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

642 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

2K+ 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

15K+ 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

2K+ 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

5K+ 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

479 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

1K+ 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

2K+ 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

826 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

2K+ 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

Previous 1 ... 3 4 5 6 7 ... 9 Next
Advertisements