Programming Articles - Page 1151 of 3363

How to convert JSON string into Lua table?

Mukul Latiyan
Updated on 20-Jul-2021 14:23:06

4K+ Views

When working with JSON we generally need to decode JSON into a string or maybe encode a string into JSON. Both of these processes of converting the string into a JSON or the opposite are seen frequently.While Lua doesn’t provide an official library to do the same, we can still make use of the third party libraries.There are many third party libraries that we can make use of, out of these the most common one is the json-lua library which can be located on this link.We can either clone the repository on our local machine and then install it or ... Read More

How to compile embedded Lua code in C?

Mukul Latiyan
Updated on 20-Jul-2021 14:20:30

2K+ Views

We know that Lua does a great job when it comes to being a simple language and an embedded language. Lua works even better with C, because of the different libraries that provide a great amount of support for it.In order to compile embedded Lua in C, we need to first write a Lua program followed by a C program that will invoke the Lua program function, and then we will compile the C program.Consider the program shown below as the Lua program −print("--I am using Lua from within C--")It should be noted that the above Lua script should be ... Read More

How to Compile a Lua Executable?

Mukul Latiyan
Updated on 20-Jul-2021 14:16:51

780 Views

While there are different ways with which a person can compile a Lua executable, some of them require more time and resources.The most basic approach is to set the Lua Path environment variable and then simply run the lua command. While this might seem to be not much of a pain, setting the Lua Path definitely requires some work.Instead of setting the Lua path, we can simply make use of the env and lua combination which will allow us to convert a Lua file into an executable or run a Lua script.The command shown below does the same −#!/USR/BIN/ENV LUAIn ... Read More

How to Call a Lua function from C?

Mukul Latiyan
Updated on 20-Jul-2021 14:15:47

3K+ Views

Calling a Lua function from C is something that requires a series of steps and a mastery in the Lua library functions. Lua provides several library functions that we can use whenever we want to call Lua functions from C or the opposite.Some of the most commonly used Lua library functions for calling a Lua function from C are −luaL_dofile(L, "myFile.lua");lua_getglobal(L, "add");lua_pushnumber(L, a);and much more.We will make use of these functions when we will call a Lua function from C.The first step is to shut down the Lua interpreter and for that we need to write a code in C.ExampleConsider ... Read More

How does Garbage Collection work in Lua Programming?

Mukul Latiyan
Updated on 20-Jul-2021 14:12:54

415 Views

Lua provides automatic garbage collection that is very helpful in providing safe memory management. It basically means that you do not need to worry about the newly created object or how to allocate memory.Lua is running a garbage collector to collect all dead objects (that is, objects that can not be accessed in Lua) to perform automatic memory management.Lua also provides us with different functions that we can use to interact with the garbage collector, these functions are −collectgarbage ( "collect") − returns a number that denotes whether the collector does a full garbage collection cycle.collectgarbage ( "count") − returns ... Read More

How do you copy a Lua table by value?

Mukul Latiyan
Updated on 20-Jul-2021 14:11:34

2K+ Views

Copying a table means that we want all the values or pairs that are present in one table in another table. In Lua, there’s no standard library function that we can use to create such a table but we can create our own function to do so.Let’s create a function in Lua that will take a table as an argument and will create a new table that will be the exact copy of the table passed as an argument to the function.ExampleConsider the example shown below as reference − Live Demoa = {} a["name"] = "mukul" a["age"] = 23 a["isWorking"] = ... Read More

Differences between Javascript and Lua programming

Mukul Latiyan
Updated on 20-Jul-2021 14:08:31

381 Views

We know that there’s a huge gap in the popularity and use-cases of JavaScript and Lua. Besides this gap in popularity and use-cases, these languages have many differences at code level.The following table highlights some of the most notable differences between JavaScript and Lua.KeyJavaScriptLuaImplicit conversion when comparingJavaScript does implicit conversion when it is comparing any two objects with either the == or != comparison operators.Lua doesn’t convert between the types when it uses the comparison operators.Operator precedenceIn JavaScript, the ==, ===, != and !== operators are of lower precedence than >, >=,

Difference between . and : in Lua programming

Mukul Latiyan
Updated on 20-Jul-2021 14:07:09

6K+ Views

The .(dot) operator in Lua is used to invoke the method of an object, and it is a widely used operator in Lua.The :(colon) operator in Lua is used when you want to pass an invisible parameter to the method of an object that you are calling.Let’s consider an example where we will have an object in which two functions are present and we will try to access these functions, first by making use of the dot operator and secondly by making use of the colon operator.ExampleConsider the example shown below − Live DemoreturnX = {foo = function(x, y) return x ... Read More

Concatenation of tables in Lua programming

Mukul Latiyan
Updated on 20-Jul-2021 14:05:05

9K+ Views

We can append two tables together in Lua with a trivial function, but it should be noted that no library function exists for the same.There are different approaches to concatenating two tables in Lua. I’ve written two approaches that perform more or less the same when it comes to complexity.The first approach looks something like this −function TableConcat(t1, t2)    for i=1, #t2 do       t1[#t1+1] = t2[i]    end    return t1 endAnother approach of achieving the same is to make use of the ipairs() function.ExampleConsider the example shown below −for _, v in ipairs(t2) do   ... Read More

Concatenation of strings in Lua programming

Mukul Latiyan
Updated on 20-Jul-2021 14:03:40

3K+ Views

Concatenation of strings is the process in which we combine two or more strings with each other, and in most of the programming languages this can be done by making use of the assignment operator.In Lua, the assignment operator concatenation doesn’t work.ExampleConsider the example shown below − Live Demostr1 = "tutorials" str2 = "point" will throw an error s = str1 + str2 print(s)Outputinput:7: attempt to add a 'string' with a 'string'Hence, the most straightforward way is to make use of the concatenation keyword which is denoted by .. (two dots)Let’s consider a few examples of the concatenation keyword in Lua.ExampleConsider ... Read More

Advertisements