is my understanding of the following code correct? - lua

suppose I have a file with name myFileName.lua, which contains the following code.
function Set(source)
set = {}
if source then
for i,v in ipairs(source) do
set[v] = true
end
end
return set
end
return Set
My understanding is as the following: source is a table structure. if source then means, if the table source is not empty, then do something. The first return set means returning the table set as the return value of the function Set. The second return Set means, returning Set function as the return value of this file myFileName.lua.
Then, in file main.lua, I have
Set = require('lib/myFileName')
This means, the Set function in the file myFile.lua is returned and assigned the name Set, so that I can use it in the file main.lua. Is this correct? Any comments are greatly appreciated.

Inside the file, Set is a function. set is a table. The if source then line will just make sure source is not nil or false. so even if source is {} empty, it will enter.
All of the rest is correct, requiring the file from main would give you access to the Set function.
For more information you can read further about logical expressions in Lua here

Related

Pass Lua table by stringified reference instead of directly by reference

I want to pass a lua reference to another function without actually using assignment = but something like loadstring.
local myTable = { test="Hello" }
local myTableStringified = tostring(myTable) -- table: 0xref
print(myTableStringified)
local myTableUnstringified = loadstring(myTableStringified)
print(myTableUnstringified) -- nil but should show table: 0xref
As seen above, this won't do.
You'll have to use one of the modules that provide serialization.
Keep in mind that loadstring returns a function that needs to be called, so to get the table back you need to use loadstring(myTableStringified)().

Lua puts of variable name instead of variable value in table

Hello guys i am trying to add Divisions to a game but there is a problem with tables, do you guys know how to solve this?
local divisonName = result3[i].name
print(divisonName)
ESX.Divisions[result3[i].owner].divisonName = {}
it's the code it should get division name and create a table with that like this (we assume divisonName gonna return swat for example):
["police"] = {
["swat"] = {
},
},
but instead of putting division name as SWAT it will put divisionName variable
i already print that divisionName variable and console return me SWAT so everything is fine with logic and value of variable but i guess there it's syntax issue i am not sure!
Console Debug image
Note that in Lua the construct some_table.field is a syntactic sugar for some_table["field"]. Whatever is written after the dot will be treated as a string key.
In your case, to index by the value stored in the variable, you need to write ESX.Divisions[result3[i].owner][divisonName], and not as .divisionName.

Initialising and using a global table

I'm very new to Lua and I'm trying to globally initialise a table at the very start of my program. At the top, I have:
storage = {}
Then, I want to iterate over elements in this table inside functions in the same file. One example is:
local output
for item in storage do
output = output .. item
end
return output
In this case, I get:
attempt to call a nil value
On the line beginning with for.
I have also tried printing out storage[1]. In this case I get:
attempt to index local 'storage' (a nil value)
Could someone please explain in simple terms what could be wrong here?
You are not showing the entire script, but it's clear that storage value gets reset somewhere between your initialization and using in for item in storage do, because if it keeps the value, you'd get a different error: attempt to call a table value.
You need to use ipairs or pairs function in the loop -- for key, item in pairs(storage) do -- but you first need to fix whatever resets the value of storage.

Lua dont allow table append

I already have a lua-table A={}, A.B={} under global variable. I have a function on the call of what creates lua table D1={}, D1.D2={}, but the porblem is this function places the table in global variable list. When i print all lua values, it prints:
A={}, A.B={}, D1=={}, D1.D2={}. Is there a way I can create the function under table under A={}, A.B={} which mean i want output as:
A={}, A.B={}, A.B.D1=={}, A.B.D1.D2={}. I dont want to use table.insert() since the hirarchy of source-table is not known.
It sounds like what you want to do here, is pass the table to the function that creates D1 and D1.D2 so you can append those values wherever you want them.
function addTable(tbl)
tbl.D1 = {}
tbl.D1.D2 = {'test'}
end
addTable(A.B)
-- now you can call A.B.D1.D2
print(A.B.D1.D2[1]) -- prints 'test'

The 'tap' method on String object doesn't return expected result

I ran in to an interesting problem while using the 'tap' method on objects of type 'String'.
"abc".tap { |o| o = "xyz" } # this line returns "abc" instead of "xyz"
The 'tap' method works on objects of other types.
[].tap { |o| o << "xyz" } # this line returns ["xyz"] as expected
I am using Rails 2.3.2 and Ruby 1.8.6 on Windows XP.
What am I missing here?
Update 1: I resolved this issue. It was an usage error on my part. In the first scenario I was re-assigning the value to the block parameter instead of modifying it. I was able to rewrite the code and get the expected result.
"abc".tap { |o| o.replace "xyz" }
Update 2: Code used here is just to demonstrate the problem. My actual code does not look like this.
Object#tap always returns the original object passed into it after executing the block, even if the block returns something different.
The benefit here is that you can put tap into the middle of a chain and test the value of an object without modifying the original value.
This means that if you pass "abc" into tap, it'll execute the block and return "abc". Your assignment to o doesn't mean anything due to scoping. Likewise, if you pass an empty array into tap, it would return an empty array back. However, in your second example, you modified the original value. Operations that modify the original such as << or gsub! or likewise will modify the object before its returned, therefore you get a different value back.
Check out http://moonbase.rydia.net/mental/blog/programming/eavesdropping-on-expressions for some more cool examples about how to use tap. Don't use it for assignment or modification or you'll get back wonky results.
Update: To your edit, why does that resolve your issue? I don't understand why you'd call tap to do a replace when you can just call replace on the object itself. What benefit is tap providing you?

Resources