Programming Articles - Page 1154 of 3363

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

string.sub() function in Lua

Mukul Latiyan
Updated on 08-Sep-2023 22:55:05

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

string.lower() function in Lua programming

Mukul Latiyan
Updated on 19-Jul-2021 12:19:22

7K+ Views

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

string.gsub() function in Lua programming

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

28K+ Views

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

string.format() function in Lua programming

Mukul Latiyan
Updated on 19-Jul-2021 12:16:06

16K+ Views

There are cases when we want to format strings which will help us to print the output in a particular format.When we use the string.format() function it returns a formatted version of its variable number of arguments following the description given by its first argument, the so-called format string.The format string that we get the output, is similar to those of the printf function of standard C: It is composed of regular text and directives, which control where and how each argument must be placed in the formatted string.Syntaxstring.format(“s = %a”)The string.format() syntax above contains an identifier s which is ... Read More

Advertisements