Filling Lua table not going as expected - lua

I am coding a little something in Lua and I've encountered a very frustrating bug/mistake in my code.
network = {}
network.neurons = {}
for i=1,4 do
network.neurons[20000] = {}
network.neurons[20000][i] = NewNeuron()
print(network.neurons[20000][i])
end
The NewNeuron() function creates a new object with some variables. The print() inside this for loop returns the table with the correct variables as expected. The problem comes when I try to use this print again in this loop:
for i=1,4 do
print(network.neurons[20000][i])
end
The print then writes 4 console lines as follows:
(no return)
(no return)
(no return)
*neuron info that should be printed*
It looks as if only the last of the 4 objects exists after I exit the creation loop. Why is this? What am I doing wrong?

You are assigning an entirely new table inside the loop when creating a NewNeuron. The declaration should be outside:
network = {}
network.neurons = {}
network.neurons[20000] = {}
for i=1,4 do
network.neurons[20000][i] = NewNeuron()
print(network.neurons[20000][i])
end

Related

Lua expected = near data

local file = 'data.txt'
local lines = lines_from(file)
global data = {} -- this line is giving the error "lua: day8.lua:20: '=' expected near 'data'"
for key, value in pairs(lines) do
local row = {}
for i=1, #value do
local char = value:sub(i,i)
row[i] = char
end
data[key] = row
end
As you can see from the code above, my code is throwing an error on the line where the variable data is initialised. This code was working earlier when I tested it, then I added more code below what is visible and it broke this line somehow.
I don't think its the code below that broke this line as otherwise why would it be showing there?
This is also my first ever code with lua so I have no experience with this language.
What could be wrong in this code that could cause this error
Global variables don't need to be declared explicitly like local ones do. The interpreter is erroring because you are prefixing global to your variable, which isn't a recognized keyword
Try the following, without the global:
data = {}

Is there a possiblity to create a variable with a string for everything inside of a table?

Just wondering about that, because i don't find a solution for it. Pretty sure because I'm new to this c: Thanks for your help.
Edit: for explanation im gonna explain it with the code a bit.
local FileList = fs.list("") --Makes a Table with all the files and directories available (as strings) on the PC it's running on (Inside of a mod called computercraft for minecraft)
for _, file in ipairs(FileList) do
--Here I need a function which assigns every string from the table to a variable, just for the explanation I'll call it unknown.function
unknown.function
end
while true do
print(a) --a is for example one of the different variables made from the "unknown.function"
sleep(1)
end
LIKE this?
AllFiles = {}
function Crazyfunction(file)
AllFiles[table.getn(AllFiles)+1] = file
end
local FileList = fs.list("")
for _, file in ipairs(FileList) do
Crazyfunction(file)
end
while true do
print(AllFiles[NUMBE_OF_FILE_HERE])
sleep(1)
end
You mean like this?
local FileList = fs.list("")
for _, file in ipairs(FileList) do
-- file is the variable which contains a string from the table.
print(file)
sleep(1)
end
What you want is... already what your loop does. You just added an extra loop for no reason.

Lua: Can I use a for loop to process all 'sometimes-nil' values in an array?

I have the following code:
local overrideNode = xml.first(rootFromOverrideXML,"nodes/node[#identifier='"..nodeIdentifier.."']")
local overrideRefNode
if (elementRef) then overrideRefNode = xml.first(rootFromOverrideXML,"nodes/node[#identifier='"..elementRef.."']") end
This results in overrideNode and overrideRefNode being nil in most cases. But either can be non-nil when something is found in the xml.
I have to handle overrideRefNode before overrideNode, so I wrote:
for _,nodeVar in ipairs {overrideRefNode, overrideNode} do
if (nodeVar) then
But it turns out that when both are non-nil, both are processed (two runs of the for loop, correct), but when overrideRefNode (the first) is nil, overrideNode (the second) is never processed (incorrect), so zero runs of the for loop, while it should be one run.
How do I loop over a set of two strings, either of which can be nil, but every non-nil must be processed?
A workaround is
local overrides = {}
overrides[1] = overrideRefNode
overrides[2] = overrideNode
for i = 1,2 do
local nodeVar = overrides[i]
-- Do some stuff here with nodeVar
end

Get name of argument of function in lua

When call a lua function like
PrintMe(MyVariableName)
I would like to be able to actually print "MyVariableName" and not it's value(well, for demo purposes).
Obviously I could just pass the string but that requires extra quotes and I also would like to print it's value.
e.g.,
MyVariable = 4
PrintVariable(MyVariable)
Would print "MyVariable is 4" or whatever
I do not want to have to duplicate the name and variable like
PrintVariable(MyVariable, "MyVariable")
as this is unnecessary duplication.
Can lua handle it?
What I'm doing now is passing the variable name in quotes and using loadstring to get the value but I would like to just pass the variable directly without the extra unnecessary quotes(which I thought debug.getlocal did but it ends up returning the value instead of the name).
Here is mock example
function printme1(var, val)
print(var.." = "..val)
end
function printme2(v)
local r
loadstring("r = "..v)() -- equivalent to r = a but must be used since v is a string representing a and not the object a
print(v.." = "..tostring(r))
end
function printme3(v)
-- unknown
end
a = 3
printme1("a", a)
printme2("a")
printme3(a)
In this case all 3 should print the same thing. printme3 obviously is the most convenient.
You can't say PrintVariable(MyVariable), because Lua gives you no way of determining which variable (if any; a constant could have been used) was used to pass an argument to your function. However, you can say PrintVariable('MyVariable') then used the debug API to look for a local variable in the caller's scope which has that name:
function PrintVariable(name)
-- default to showing the global with that name, if any
local value = _G[name]
-- see if we can find a local in the caller's scope with that name
for i=1,math.huge do
local localname, localvalue = debug.getlocal(2,i,1)
if not localname then
break -- no more locals to check
elseif localname == name then
value = localvalue
end
end
if value then
print(string.format("%s = %s", name, tostring(value)))
else
print(string.format("No variable named '%s' found.", name))
end
end
Now you can say:
PrintVariable('MyVariable')
While in this case will print "MyVariable = 4".
Not, if you really want to do this without the quotes, you could check the caller's locals for variables that have a supplied value, but that's occasionally going to give you the wrong variable name if there is more than one variable in the caller's scope with a given value. With that said, here's how you'd do that:
function PrintVariable(value)
local name
-- see if we can find a local in the caller's scope with the given value
for i=1,math.huge do
local localname, localvalue = debug.getlocal(2,i,1)
if not localname then
break
elseif localvalue == value then
name = localname
end
end
-- if we couldn't find a local, check globals
if not name then
for globalname, globalvalue in pairs(_G) do
if globalvalue == value then
name = globalname
end
end
end
if name then
print(string.format("%s = %s", name, tostring(value)))
else
print(string.format("No variable found for the value '%s'.", tostring(value)))
end
end
Now you can say PrintVariable(MyVariable), but if there happened to be another variable in the caller's scope with the value 4, and it occurred before MyVariable, it's that variable name that will be printed.
you can do stuff like this with the debug library... something like this does what you seem to be looking for:
function a_func(arg1, asdf)
-- if this function doesn't use an argument... it shows up as (*temporary) in
-- calls to debug.getlocal() because they aren't used...
if arg1 == "10" then end
if asdf == 99 then end
-- does stuff with arg1 and asdf?
end
-- just a function to dump variables in a user-readable format
function myUnpack(tbl)
if type(tbl) ~= "table" then
return ""
end
local ret = ""
for k,v in pairs(tbl) do
if tostring(v) ~= "" then
ret = ret.. tostring(k).. "=".. tostring(v).. ", "
end
end
return string.gsub(ret, ", $", "")
end
function hook()
-- passing 2 to to debug.getinfo means 'give me info on the function that spawned
-- this call to this function'. level 1 is the C function that called the hook.
local info = debug.getinfo(2)
if info ~= nil and info.what == "Lua" then
local i, variables = 1, {""}
-- now run through all the local variables at this level of the lua stack
while true do
local name, value = debug.getlocal(2, i)
if name == nil then
break
end
-- this just skips unused variables
if name ~= "(*temporary)" then
variables[tostring(name)] = value
end
i = i + 1
end
-- this is what dumps info about a function thats been called
print((info.name or "unknown").. "(".. myUnpack(variables).. ")")
end
end
-- tell the debug library to call lua function 'hook 'every time a function call
-- is made...
debug.sethook(hook, "c")
-- call a function to try it out...
a_func("some string", 2012)
this results in the output:
a_func(asdf=2012, arg1=some string)
you can do fancier stuff to pretty this up, but this basically covers how to do what you're asking.
I have bad news, my friend. You can access function parameter names as they appear at the top of the function, but the data to access exactly what they were named in the calling function does not exist. See the following:
function PrintVariable(VariableToPrint)
--we can use debug.getinfo() to determine the name 'VariableToPrint'
--we cannot determine the name 'MyVariable' without some really convoluted stuff (see comment by VBRonPaulFan on his own answer)
print(VariableToPrint);
end
MyVariable = 4
PrintVariable(MyVariable)
To illustrate this, imagine if we had done:
x = 4
MyVariable = x
MyOtherVariable = x
x = nil
PrintVariable(MyVariable)
Now if you were Lua, what name would you attach in the metadata to the variable that ends up getting passed to the function? Yes, you could walk up the stack with debug.getint() looking for the variable that was passed in, but you may find several references.
Also consider:
PrintVariable("StringLiteral")
What would you call that variable? It has a value but no name.
You could just use this form:
local parms = { "MyVariable" }
local function PrintVariable(vars)
print(parms[1]..": "..vars[1])
end
local MyVariable = "bar"
PrintVariable{MyVariable}
Which gives:
MyVariable: bar
It isn't generic, but it is simple. You avoid the debug library and loadstring by doing it this way. If your editor is any good, you could write a macro to do it.
Another possible solution is add this facility your self.
The Lua C API and source is pretty simple and extendable.
I/we don't know the context of your project/work but if you ARE making/embedding your own Lua build you could extend the debug library with something to do this.
Lua passes it's values by reference, but unknown offhand if these contain a string name in them and if so if easily accessible.
In your example the value declaration is the same as:
_G["MyVariable"] = 4
Since it's global. If it were declared local then like others stated here you can enumerate those via debug.getlocal(). But again in the C context of the actual reference context it might not matter.
Implement a debug.getargumentinfo(...) that extends the argument table with name key, value pairs.
This is quite an old topic and I apologize for bringing it back to life.
In my experience with lua, the closest I know to what the OP asked for is something like this:
PrintVariable = {}
setmetatable(PrintVariable, {__index = function (self, k, v) return string.format('%s = %s', k, _G[k]) end})
VAR = 0
VAR2 = "Hello World"
print(PrintVariable.VAR, PrintVariable.VAR2)
-- Result: VAR = 0 VAR2 = Hello World
I do not give more explanation, because the code is quite readable, however:
What happens here is simple, you only set a metatable to the PrintVariable variable and add the __index metamethod that is called when the table is forced to search for a value in its index, thanks to this functionality you can achieve what you see in the example.
Reference: https://www.lua.org/manual/5.1/manual.html
I hope that future and new visitors will find this helpful.

Using tables in other files

Edit I got this working, I'm not sure if this is the right way to do it, but this is what works right now
I just started learning Lua, and I'm trying to figure out how to pass tables between files so that I can have a more organized codespace. I have read through the book Programming in Lua, and for some reason, I can't figure out what i'm doing wrong.
The problem i'm getting is this error:
lua: Test2.lua:3: attempt to call method 'New' (a nil value)
From this code:
--Test.lua----------------
module("Test", package.seeall)
vector = require "./Hump/vector"
Bot = {}
Bot.position = vector.new(0,0)
function Bot:New(object)
object = object or {}
setmetatable(object, self)
self.__index = self
return object
end
--Test2.lua------------------
require "Test"
Bot1 = Test.Bot:New()
print(Bot1.position)
As far as I understand it, this error means that it cannot find the method new, it is effectively undefined. I thought that require imports the file in the path?
Bot is an empty table.
local B = {} -- initialize local B with new table
Bot = B -- Bot now references the same table as B
B = { position = vector.new(0,0) } -- here you create a NEW table, B ~= Bot now
function B:New(object) -- store New function in B table, Bot still empty
So you're returning an empty table.
No need for two variables here at all.
local Bot = {
-- stuff
}
function Bot:New(object)
-- stuff
end
return Bot

Resources