The following code works as expected:
local t = {}
print(t[1])
The above will print nil.
How come the following code results in an error?
print({}[1])
What is the logic behind this?
You can:
print(({"a", "b", "c"})[2]) -- "b"
Related
Alright I'll get straight to the point. I am currently working on a script for the RedM and I have run into a bit of trouble. I have a table I need to call but I need to be able to call it with a string and despite trying to figure this out for days I have had no luck. Here's a quick example of what I am trying to do.
Table = {
"Value 1",
"Value 2",
"Value 3"
}
local _Table = "Table"
for key, value in pairs(_Table) do
print(value)
end
What I am wanting that to do is print out everything in that table however when you try to use a string you get the following error
$lua main.lua
lua: main.lua:9: bad argument #1 to 'for iterator' (table expected, got string)
stack traceback:
[C]: in function 'next'
main.lua:9: in main chunk
[C]: in ?
And obviously the script works when you don't use a string however that won't work for me. To be totally honest I'm not even sure what I'm trying to do is even possible, but if it is and anyone has a solution it would be VERY appreciated lol.
If I understand correctly, you want to get a table by its variable name. This is only possible at all if the variable is global, which it shouldn't (Always make your variables local, children) and even then it wouldn't be the most elegant solution.
A better alternative might be: put your tables in another table:
local Tables = {
Table_1 = { 1, 2, 3, }
Table_2 = { 2, 3, 4, }
}
print(Tables["Table_1"])
This keeps the global scope clean and allows you to pass the whole set of tables around as a single value.
you cannot call a table unless its metatable implements __call. I assume you're confusing some basic terminology here.
your code doesn't make sense
-- this line assigns the string "Table" to _Table
local _Table = "Table"
-- now you put into pairs which may only take a table as input
for key, value in pairs(_Table) do
print(value)
end
If Table is a global variable as in your example you can access it through the global environment table _G.
for key, value in pairs(_G.Table) do print(value) end
or
local _Table = "Table"
for key, value in pairs(_G[_Table]) do print(value) end
I am currently doing 2 steps in my code and I just realized I can combine both steps in a LUA script.
I am doing:
SPOP on my set
calling a lua script to do other things.
The value from step#1 is being passed and stored in the local variable ele.
My lua script looks like:
local ele = KEYS[1]
local p = KEYS[2]
local u = KEYS[3]
if redis.call("SISMEMBER", u, ele) == 0 then
..
..
return "OK"
else
return "EXISTS"
end
How can I call SPOP from inside my lua script and store it in a variable.
I need to do:
local popped = redis.call("SPOP", "my-set-here")
I'm not sure if that will work, but then I have to check if it is null or has a value I guess. Just want to make sure I am following best practise.
BTW, as a side note, what is the fastest way to create and test lua scripts?
You can check the value of popped for non-nillness with something like:
if popped then
-- do something
end
As for developing Redis Lua scripts, have a look at Zerobrane's integration:
http://notebook.kulchenko.com/zerobrane/redis-lua-debugging-with-zerobrane-studio
https://redislabs.com/blog/zerobrane-studio-plugin-for-redis-lua-scripts/
Disclosure: I was involved in the integration effort ;)
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
So, I’m having an issue while trying to split strings into tables (players into teams). When there are two players only, it works like a charm, but when there are 3+ players, this pops up: “Init Error : transformice.lua:7: bad argument: table expected, got nil”. Everything seems to be ok, I really don’t know what’s wrong. Can you guys please help me? Thanks! Here is my code:
ps = {"Player1","Player2","Player3","Player4"}
local teams={{},{},{}}
--[[for name,player in pairs(tfm.get.room.playerList) do
table.insert(ps,name)
end]]
table.sort(ps,function() return math.random()>0.5 end)
for i,player in ipairs(ps) do
table.insert(teams[i%#teams],player)
end
Lua arrays start at index 1, not 0. In the case of when you have 3 players this line:
table.insert(teams[i%#teams],player)
Would evaluate to:
table.insert(teams[3%3],player)
Which then would end up being:
table.insert(teams[0],player)
And teams[0] would be nil. You should be able to write it as:
table.insert(teams[i%#teams+1],player)
instead.
I have a simple problem: I would like to cause the print function in lua to print the contents of a table, rather than just the word "table" and a memory address. For example:
> tab = {}
> tab[1]="hello"
> tab[2]="there"
>
> print(tab)
table: 0x158ab10
--should be
1 hello
2 there
I am aware that I can get this effect by executing something like:
for i,v in pairs(tab) do print(i,v) end
but I would like it to happen simply when I execute print(tab) rather than having to write out a loop every time. can this be done?
You would need to set __tostring() on every table you created. An easier way would be to use a pretty printing technique.
See this link: http://lua-users.org/wiki/TableSerialization
You can do this by overriding the global tostring() function. This is what print() calls on its arguments.
If you do not want to do any coding, try out the Microlight library by Steve Donovan. You can use it as follows:
tostring = require "ml".tstring
tab = {"abc", 3.14, print, key="value", otherkey={1, 2, 3}}
print(tab) --> {"abc",3.14,function: 0x7f5a40,key="value",otherkey={1,2,3}}