I have a lua file whose content is lua Table as below:
A={},
A.B={},
A.B.C=0;,
The problem is I want to add prefix XYZ before each above statements. So after the parse the database should have something loke this:
XYZ.A={},
XYZ.A.B={},
XYZ.A.B.C={},
Any ideas? Thanks in advance
You can load the file with XYZ as is environment: loadfile("mydata","t",XYZ). See loadfile in the manual.
This works in Lua 5.2. For Lua 5.1, use loadfile followed by setfenv.
If you can afford polluting your global space with A, simply assign it later:
-- load the file
-- if XYZ doesn't exist, XYZ = { A = A } would be probably shorter
XYZ.A = A
A = nil
I think this is what you want:
XYZ = {}
XYZ.A = {}
XYZ.A.B = {}
XYZ.A.B.C = 0
How about you simply do:
XYZ = {
A = {
B = {
C = 0
}
}
}
If you don't want to nest objects so deep then you may do:
XYZ = {
A = A
}
A = nil
This assumes that you have already declared the object A before.
Related
Sorry in advance if this is an incorrect question. I'm fairly new to Lua and I'm not sure how to go about this. I want to access a variable stored in a table from a function variable.
As far as I know there is no self-referencing tables before constructed.
An example would be this:
local bigTable = {
a = {
foo = 0,
bar = function(y)
print(foo) --Incorrect
end
}
}
What would be the best approach for this situation?
What you want to do is to create a table first, and append the keys to it:
local a = {}
a.foo = 0
a.bar = function()
print(a.foo)
end
local bigTable = {
a = a
}
bigTable.a.bar() -- prints 0
local bigTable = {
a = {
foo = 0,
bar = function(self, ...)
print(self.foo)
end,
}
}
-- Somewhere else in the code...
bigTable.a.bar(bigTable.a) --> 0
-- or the shorter but (almost) equivalent form:
bigTable.a:bar() --> prints 0
I anticipate your next question will be "What does the : do?", and for that there's lots of answers on SO already :)
Note that there's potential for a performance improvement here: if the above code gets called a lot, the bar method will be created again and again, so it could make sense to cache it; but that's pointless unless the surrounding code is already fast enough that this one allocation would have a noticeable impact on its runtime.
I have a main lua file app.lua , and in that app I have a button to "load data" in.
NOTE: LUA 5.1 not 5.2
The data file is a lua file as well with tables in it.
data1 = {
{a,b,c},
{1,2,3}
}
data2 = {
{d,e,f}
}
The goal was to make those tables available to the app anytime I choose to load the file.
I tried the example from the lua site
function dofile (filename)
local f = assert(loadfile(filename))
return f()
end
but f() is just printing a massive string. I can't seem to access f.data1[1] for example.
The file you're loading is not a data table. That's a piece of code, anonymous function that is executable. You run that code in return f() statement.
But see what that code does - it doesn't return anything. Instead it assigns two global variables, data1 and data2. You can access those as data1[1] for example.
You could return the data in the file being loaded, that way it wouldn't pollute the global environment, and probably will look like you imagined it to be:
return {
data1 = { {a,b,c}, {1,2,3} },
data2 = { d,e,f}
}
And in other file:
local f = assert(loadfile(filename))
my_data = f()
print(my_data.data1[1][1])
I have File: data.lua
#! /usr/bin/env lua
local a = {
b = {
c = {
version = "z.y"
},
d = {
version = "z.w"
},
getcversion = function ( self )
print( self.c.version )
end
}
}
Now I need to 'getcversion()' Function..
a.b:getcversion()
The problem is, I need to call it from the outside:
data.lua "a.b:getcversion()"
I tried everything that I could, but I couldn't solve this..
Does any one knows how I could call 'getcversion()' ?
Thanks in Advance,
Regards
You'll need to add something like the following to your script:
load(arg[1], "<string>", "t", {a = a})()
load will load the content of the passed parameter (arg[1]) and will return the function that will execute that code in a specific environment (provided as {a = a} table), as you need to pass the values of local variables to your code (Lua 5.2+).
Keep in mind that this will allow the caller to pass arbitrary Lua code to your script, which may be a security issue.
The error handling is left as the exercise for the reader.
Add return a to the end of data.lua to turn it into a module, and then:
$ lua -e 'a = require("data"); print(a.b:getcversion())'
z.y
I have a variable in groovy like below:
project.Map
{
time.'1 1 * ?' = ['T1']
time.'2 1 * ?' = ['T2']
templates.'T1' = ['Z','X','Y']
templates.'T2' = ['Q']
}
Sorry but I am new to groovy ,when i try to access the individual
variable values in project.map how do i access them
i tried something like below
log.info(grailsApplication.config.project.Map.time[1])
log.info(grailsApplication.config.project.Map.get('time.'2 1 * ?'' ))
log.info(grailsApplication.config.project.Map.get('time[0]' ))
log.info(grailsApplication.config.project.Map.time.get('1 1 * ?'))
but they all print null value or object references.how do i access values for
time and templates both within a for loop and without it.
please see http://grails.org/doc/latest/guide/conf.html#config for the ways the config is allowed to nest. your outer syntax is especially mentioned to not be allowed:
However, you can't nest after using the dot notation. In other words, this won't work:
// Won't work!
foo.bar {
hello = "world"
good = "bye"
}
You have to write it as
project { Map { ... } }
The inner dotted parts (with the assignment) are ok (according to the doc)
I am using corona (lua) with parse.com and I have hit a problem constructing an $in query using values from another table / array.
My code is a little like this:
local usersToFetch = {}
table.insert( usersToFetch, "KnVvDiV2Cj")
table.insert( usersToFetch, "Paf6LDmykp")
and the working query I want to perform is the following lua table (which will get encoded before heading to parse). As I said, this works when I am hard coding the values as shown
local queryTable = {
["where"] = {
["objectId"] = { ["$in"] = {"KnVvDiV2Cj","Paf6LDmykp" }}
},
["limit"] = 1000
}
My problem is how do I incorporate my 'usersToFetch' table in the above table so that it will work the same as hard coding the values?
I swore I tried that but clearly I did not.. I think that I placed it inside the braces whereas they are not needed which is where I went wrong.
Thanks, hjpotte92 - what you put worked fine but this is my final solution in a single declaration:
Where I was going wrong before was because I had too many braces ["objectId"] = { ["$in"] = { usersToFetch } }
local queryTable = {
["where"] = {
["objectId"] = { ["$in"] = usersToFetch}
},
["limit"] = 1000
}