Found 33676 Articles for Programming

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

What is the difference between pairs() vs. ipairs() in Lua?

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

11K+ Views

In Lua, we make use of both the pairs() and ipairs() function when we want to iterate over a given table with the for loop. Both these functions return key-value pairs where the key is the index of the element and the value is the element stored at that index table.While both of them have some similarities, it is also good to know that they have some very notable differences that we should be aware of.The first difference between the pairs() and ipairs() function is that the pairs() function doesn’t maintain the key order whereas the ipairs() function surely does.ExampleConsider ... Read More

What does operator ~= mean in Lua?

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

1K+ Views

The ~= symbol or operator in Lua is known as the not-equal to operator. In many programming languages you might have seen the symbol != which is also known as the not equals operator.Let’s consider a few examples where we can make use of the not equals operator.ExampleConsider the examples shown below − Live Demoa = 2 b = 3 print(a ~= b)OutputtrueExample Live Demot1 = {4,2,3} t2 = {2,3,4} print(t1 ~= t2)Outputtrue

What does # mean in Lua programming?

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

817 Views

The unary operator # is known as the Length operator in Lua. It is used almost everywhere in Lua. By everywhere, I meant that anywhere we would require to calculate the length of the any string or can also be used in tables also, but when it comes to table, it is generally not preferred to use the # operator as it doesn’t calculate the number of elements present inside the table.Let’s explore different examples of the length operator to understand how we can make use of it.ExampleConsider the example shown below − Live Demoprint(#"abcdefg") print(#{"a", "b", "c", 77})Output7 4In the ... Read More

What are some of the important Scientific Libraries used in Lua programming?

Mukul Latiyan
Updated on 19-Jul-2021 11:25:40

369 Views

While we know that Lua does a great job when we want to use it as an embedded language, it can also exceed its basic uses and can be used in extreme cases such as Machine Learning and statistical analysis.There are many scientific libraries that are present in the market for this particular case of making more out of Lua. Let’s explore what these libraries are and what they do.The first name that comes to my mind when talking about Lua and machine learning in the same sentence is of the Torch project. The torch project is a scientific computing ... Read More

What are Closures in Lua Programming?

Mukul Latiyan
Updated on 19-Jul-2021 11:23:46

1K+ Views

In Lua, any function is a closure. In a narrower sense, a closure is an anonymous function like the returned function in your example.Closures are first-class: they can be assigned to variables, passed to functions and returned from them. They can be both keys and values in Lua tables.Unlike C++ or PHP, closures in Lua have access to all variables in local scope— upvalues with no need to declare upvalues explicitly. Upvalues survive when code execution leaves the block where they were set.Now that we know what a closure is and why it is useful, let’s take an example and ... Read More

The __index metamethod in Lua Programming

Mukul Latiyan
Updated on 19-Jul-2021 11:20:29

2K+ Views

Whenever we try to access a field that hasn’t been declared in a table in Lua, the answer we get is nil. While this is true, but the reason for it is that when such access happens, the interpreter triggers a search for an __index metamethod and if it doesn’t find any method named __index, then we get nil as an answer; else we will get whatever the value of the field is set in the __index metamethod.We can explicitly put in the __index method in a table and provide it the named values that we want it to return ... Read More

table.unpack() function in Lua programming

Mukul Latiyan
Updated on 19-Jul-2021 11:17:40

16K+ Views

When we want to return multiple values from a table, we make use of the table.unpack() function. It takes a list and returns multiple values.Syntaxtable.unpack{x, y, z, ....}ExampleThe table.unpack() function provides us with all the values that are passed to it as an argument, but we can also specify which value we want by following the example shown below − Live Demoa, b = table.unpack{1, 2, 3} print(a, b)In the above example, even though the table.unpack() function contains different values, namely 1, 2 and 3, we only are storing the first two values, i.e., a and b and the value 3 ... Read More

table.pack() function in Lua programming

Mukul Latiyan
Updated on 19-Jul-2021 11:16:16

6K+ Views

When we want to return a table as a result from multiple values passed into a function, then we make use of the table.pack() function. The table.pack() function is a variadic function.Syntaxtable.pack(x, y, z, ....)ExampleThe table.pack() function provides a table formed with all the values that are passed to it as an argument, consider the example shown below − Live Demoa = table.pack(1, 2, 3) print(a) print(a.n)In the above example, we passed three numbers to the table.pack() function as an argument and then we are printing the returned value, i.e., which will hold the address of the table that contains the ... Read More

How to create violin plot for categories with grey color palette using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 13-Aug-2021 09:14:41

336 Views

To create violin plot for categories with grey color palette using ggplot2, we can follow the below steps −First of all, create a data frame.Then, create the violin plot for categories with default color of violins.Create the violin plot for categories with color of violins in grey palette.Creating the data frameLet's create a data frame as shown below − Live Demo> Group Score df dfOn executing, the above script generates the below output(this output will vary on your system due to randomization) −     Group  Score 1   Second   405 2    Third   947 3    First    78 ... Read More

Advertisements