Programming Articles - Page 1156 of 3366

Passing Lua script from C++ to Lua

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

603 Views

The idea of passing Lua script from C++ to Lua includes the fact that we will have to load the libraries and header files as Lua is ANSI C, and if we are coding in C++, we will need to enclose the #includes in extern “C”.The old and mostly used approach would be to load the libraries that Lua provides and then simply call the C++ function from Lua.In order to load the script from C++ to Lua, we need to set up and shutdown the Lua interpreter and we can do that with the help of the following code.ExampleConsider ... Read More

math.modf() function in Lua programming

Mukul Latiyan
Updated on 19-Jul-2021 11:58:30

3K+ Views

There are several occurrences when we want to get the integer value of a number and also the fractional value that the number has if any, so that we can use either or both of these values.Lua provides us with a math.modf() function that we can use to find the integer value along with the fractional value if the number has any.Syntaxmath.modf(number)The math.modf() function returns two values when we call the function, the first value is the integer value of the number and the second returned value is the fractional value of the number if it has any.ExampleLet’s consider a ... Read More

math.min() function in Lua

Mukul Latiyan
Updated on 19-Jul-2021 11:56:18

8K+ Views

There are several occurrences when we want to get the min value from a given series of numbers and then use that value later on.The Minimum value from the series of different numbers is the value that is the minimum of all the numbers that are present in that series.Lua provides us with a math.min() function that we can use to find the min value out of different numbers that we pass to it as an argument.ExampleLet’s consider a simple example where we will make use of the math.min() function in Lua − Live Demoa = 10 b = 11 c ... Read More

math.max() function in Lua programming

Mukul Latiyan
Updated on 19-Jul-2021 11:53:24

10K+ Views

There are several occurrences when we want to get the max value from a given series of numbers and then use that value later on.The Maximum value from the series of different numbers is the value that is the maximum of all the numbers that are present in that series.Lua provides us with a math.max() function that we can use to find the max value out of different numbers that we pass to it as arguments.ExampleLet’s consider a simple example where we will make use of the math.max() function in Lua − Live Demoa = 10 b = 11 c = ... Read More

math.floor() function in Lua programming

Mukul Latiyan
Updated on 19-Jul-2021 11:47:04

13K+ Views

There are several occurrences when we want to get the floor value of an integer to round it off and then use that value later on. The floor value of a number is the value that is rounded to the closest integer less than or equal to that integer. Lua provides us with a math.floor() function that we can use to find the floor value of a number.ExampleLet’s consider a simple example where we will make use of the math.floor() function in Lua − Live Demoa = math.floor(3.3) b = math.floor(7.1) print(a) print(b)Output3 7It should be noted that if we try ... Read More

math.ceil() function in Lua programming

Mukul Latiyan
Updated on 19-Jul-2021 11:45:58

6K+ Views

There are several occurrences when we want to get the ceil value of an integer to round it off and then use that value later on.The Ceiling value of a number is the value that is rounded to the closest integer greater than or equal to that integer.Lua provides us with a math.ceil() function that we can use to find the ceil value of a number.ExampleLet’s consider a simple example where we will make use of the math.ceil() function in Lua − Live Demoa = math.ceil(3.3) b = math.ceil(7.1) print(a) print(b)Output4 8ExampleIt should be noted that if we try to find ... Read More

Lua pattern matching vs regular expression

Mukul Latiyan
Updated on 19-Jul-2021 11:43:09

1K+ Views

It is known that the design of the pattern matching that Lua follows is very much different, then the regular expression design which is generally based on POSIX.They have very less in common, and the more popular approach would be POSIX out of the two because it works great when the examples become more complex and it can handle variety of cases, but this does not mean that the Lua’s pattern matching is bad. In fact, it is easier to understand and it works like a charm too.Instead of using regex, the Lua string library has a special set of ... Read More

Lexical Conventions in Lua Programming

Mukul Latiyan
Updated on 19-Jul-2021 11:42:06

432 Views

In this article, we will learn how to declare and write different lexical conventions in Lua programming.In Lua, we call NAMES as IDENTIFIERS and they can be any string of letters, digits, and underscored, and they should not begin with a digit.Let’s consider an example of different Identifiers in Lua and see which ones are valid and which aren’t.ExampleConsider the example shown below − Live Demoi = 10 print(i) j1 = 11 print(j1) _ij = 99 print(_ij) aVeryLongName = "Tutorials point" print(aVeryLongName)In the above example, all the variables (Identifiers) are valid, as they either start with letters, digits, ... Read More

Why does Lua have no “continue” statement?

Mukul Latiyan
Updated on 19-Jul-2021 11:36:51

5K+ Views

There’s no continue statement in Lua, and it’s not because the developers of the Lua programming language felt that it is of no use, in fact, in the official documentation they mentioned “continue was only one of a number of possible new control flow mechanisms”. This clearly shows that the developers of the Lua programming language aren’t a huge fan of the “continue” statement.If we need to break it down the fact that why Lua developers doesn’t wanted a “continue” statement, then it comes down to two possible answers, these are −They think that the “continue” statement is just another ... Read More

Why do Lua arrays (tables) start at 1 instead of 0?

Mukul Latiyan
Updated on 19-Jul-2021 11:34:35

2K+ Views

Yes, the arrays in Lua start with index 1 as the first index and not index 0 as you might have seen in most of the programming languages.ExampleConsider the example shown below − Live Demoarr = {10, 11, 12, 13, 14} print(arr[0]) print(arr[1])In the above example, we are printing the values at indices 0 and 1, and by default, the value at the 0 index will be nil, as in Lua, the arrays start indexing from 1, not 0.Outputnil 10So, now we know that the arrays are 1-index based not 0-index based. But, wait, why 1-index and 0-index?Actually, there have been ... Read More

Advertisements