"for" about table in Lua - lua

tablelogin = {0 = "test1",1 = "test2",2 = "test3",3 = "test4"}
for pp=0,#table do
if takeinternalogin == (tablelogin[pp]) then
LogPrint("Login found")
else
LogPrint("failed login not found")
end
end
takeinternalogin is an internal function of my program that takes the person's login.
In this script I'm taking the person's login and comparing whether or not the login is in the table.
It works, but after the else if the person's login is not in the table, it returns the message "failed login not found" 4 times, that is, it returns the number of times it checks the table.
I don't understand. How can I make the message execute only 1 time?

first of all table is Lua's table library. You should not use it as a variable name. Unless you've added numeric fields to that table or replaced it with something else #table should be 0 and hence your loop should not do anything.
But as you say your loop runs 4 times I suppose you modified table.
You say internallogin is a function so you can never enter the if block as you compare a function value cannot equal a string value: takeinternalogin == (tablelogin[pp] is always false! takeinternallogin would have to return a string.
Why you're using #table here in the first place is unclear to me.

You are currently printing the error message every time it iterates over the table and the current value does not match.
local arr = {[0] = "test1", [1] = "test2", [2] = "test3", [3] = "test4"}
function findLogin(input)
for i,v in pairs(tablelogin) do
if v == input then
LogPrint("Login found")
return i
end
end
LogPrint("failed login not found")
end
login = findLogin(takeinternalogin)
Using return within a loop makes it break out of the loop and in this case never reach the line where it prints the error.

Related

Lua: Passing index of nested tables as function arguments?

Is it possible to have a function that can access arbitrarily nested entries of a table?
The following example is just for one table. But in my real application I need the function to check several different tables for the given (nested) index.
local table1 = {
value1 = "test1",
subtable1 = {
subvalue1 = "subvalue1",
},
}
local function myAccess(index)
return table1[index]
end
-- This is fine:
print (myAccess("value1"))
-- But how do I access subtable1.subvalue1?
print (myAccess("subtable1.subvalue1???"))
You won't be able to do this using a string unless you use load to treat it as Lua code or make a function to walk on a table.
You can make a function which will split your string by . to get each key and then go one by one.
You can do this using gmatch + one local above gmatch with current table.
#Spar: Is this what you were suggesting? It works anyway, so thanks!
local table1 = {
value1 = "test1",
subtable1 = {
subvalue1 = "subvalue1",
},
}
local function myAccess(index)
local returnValue = table1
for key in string.gmatch(index, "[^.]+") do
if returnValue[key] then
returnValue = returnValue[key]
else
return nil
end
end
return returnValue
end
-- This is fine:
print (myAccess("value1"))
-- So is this:
print (myAccess("subtable1.subvalue1"))

Associate string with function Lua

I have table like a:
{ "video", video_search ( message ) }.
How can i make check if input string = "video" then execute video_search command?
The correct answer to your question would be
if input_string == "video" then
video_search()
end
but going by the title and a fair bit of intuition, I assume you really want to ask
how do I associate functions with strings and, given a string, call the associated function?
To which the answer is a different one: first, restructure your table so it looks like this
local whatever = {
["video"] = video_search;
["audio"] = audio_search;
-- whatever else you have...
}
then you can just call the function like this:
local input = "video"
local message = "whatever a message is in your program"
whatever[input](message)

Obtain values from embedded tables Lua

I am new to Lua and I am trying to learn how to make a function with embedded tables. I am stuck trying to figure out a way to make the function meet specific values in the table.
Here is an example of a table:
TestTable = {destGUID1 = {catagory1 = {A=1,B=5,C=3},catagory2 = {A=5,B=3,C=2}},destGUID2 = {catagory1 = {A=1,B=5,C=3},catagory2 = {A=5,B=3,C=2}}}
Now I want to make a function for this table that pulls values only from the specific destGUID. Like:
function CatInfo(GUID,Cat)
for i=1, #TestTable do
if TestTable[i] == GUID then
for j=1, TestTable[i][GUID] do
if TestTable[i][GUID][j] == Cat then
return TestTable[i][GUID][Cat].A -- returns value "A"
end
end
end
end
end
So that when I use this function, I can do something like this:
CatInfo(destGUID2,catagory1) -- returns "1"
Given your table structure, you don't need to do any looping; you can simply return the value from the table based on GUID and the category:
TestTable = {
destGUID1 = {catagory1 = {A=1,B=5,C=3},catagory2 = {A=5,B=3,C=2}},
destGUID2 = {catagory1 = {A=1,B=5,C=3},catagory2 = {A=5,B=3,C=2}}
}
function CatInfo(GUID,Cat)
return TestTable[GUID][Cat].A
end
print(CatInfo('destGUID2','catagory1'))
This will print 1. Note that destGUID2 and catagory1 need to be in quotes as those are strings.

Displaying Lua tables to console by concatenating to string

I was wondering whether it is possible to display tables in the console. Something like:
player[1] = {}
player[1].Name = { "Comp_uter15776", "maciozo" }
InputConsole("msg Player names are: " .. player[1].Name)
However, this is obviously wrong as I receive the error about it not being able to concatenate a table value. Is there a workaround for this?
Much thanks in advance!
To turn an array-like table into a string, use table.concat:
InputConsole("msg Player names are: " .. table.concat(player[1].Name, " "))
The second argument is the string placed between each element; it defaults to "".
to make life easier on yourself for this... i'd recommend naming elements in the inner tables as well. this makes the code above easier to read when you need to get at specific values in a table that are meaningful for some purpose.
-- this will return a new instance of a 'player' table each time you call it.
-- if you need to add or remove attributes, you only need to do it in one place.
function getPlayerTable()
return {FirstName = "", LastName = ""}
end
local players = {}
local player = getPlayerTable()
player.FirstName = "Comp_uter15776"
player.LastName = "maciozo"
table.insert(players, player)
... more code to add players ...
local specific_player = players[1]
local specific_playerName = specific_player.FirstName..
" ".. specific_player.LastName
InputConsole("msg Some message ".. specific_playerName)

how do i iterate the tables parameters which is present under the main table?

In lua ,im calling a function which returns a table variable that contains many parameter internally..but when i get that value i couldnt access the paramter which is present in the table. I can see the tables parameter in the original function in the form of
[[table:0x0989]]
{
[[table:0x23456]]
str = "hello"
width = 180
},
[[table:0x23489]]
{
str1 = "world"
}
it shows like this.but when it returns once i can able to get the top address of table like [[table:0x0989]]..when i tried acessing the tables which is present inside the main table.it is showing a nil value...how do i call that ?? can anyone help me??
If I'm reading it correctly you're doing this:
function my_function ()
--do something
return ({a=1, b=2, c=3})
end
From that you should be able to do this:
my_table = my_function()
then
print(my_table.a) --=> 1
print(my_table.b) --=> 2
print(my_table.c) --=> 3

Resources