This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
smurfofiel's profile picture

Specific Table Location

Started by smurfofiel, 04 October 2013 - 08:48 AM
smurfofiel #1
Posted 04 October 2013 - 10:48 AM
Hi i have a problem this is my code so far:

local user = "user1"

local uTable = {
[1] = "user1",
[2] = "user2",
}

for k, v in pairs(uTable) do
  if user == uTable[v] then
	print(k)
	break
  end
end

basically what i want to do is: search if a user is in the username table and then check at what specific location he is at. for example i check if user1 is in the table and then i want to know he is the first one in the table. how do i do that?
theoriginalbit #2
Posted 04 October 2013 - 11:07 AM
You were close with your loop that you provided, what you've done wrong is that v is actually the value in the table, k is the key, therefore you want to do the following

for k,v in pairs(uTable) do
  if user == uTable[k] then
	print(k)
	break
  end
end
However, what's the point of looking up the value again, when we already have it, so instead we can compare against v.

for k,v in pairs(uTable) do
  if user == v then
	print(k)
	break
  end
end
Also you would want to actually store the index for later, something like so

local userIdx
for k,v in pairs(uTable) do
  if user == v then
    userIdx = k
    break
  end
end

--# you now know out here which index they were from the userIdx variable

Lastly I have a feeling this is going to be used as some kind of login system, and that you're using the position the user is in the table to retrieve their password from another table? Yes? If so then I suggest doing the following change to your table layout/structure, its much simpler and requires no loops meaning that it is far more efficient.

local uTable = {
  ["user1"] = "user password",
  ["user2"] = "qwerty",
  ["user3"] = "S3cure Pa55w0rD
}
The reason that the above table structure is so much better is that we can quickly and easily perform lookups on the table, like so

local user = "user1"
local pass = uTable[user]
In the above code the pass variable will either contain the user's password, or if that user didn't exist it will be nil. Expending upon the above method can make for some very powerful, extensible, and efficient login systems (all be it not very secure having hardcoded usernames/passwords in the table, but that's a different topic).
Edited on 04 October 2013 - 09:09 AM
smurfofiel #3
Posted 04 October 2013 - 11:36 AM
thanks but actually i want to make a search system where you can look up certain people in a table and get their position :)/> but this helps me out a lot thanks
smurfofiel #4
Posted 04 October 2013 - 11:38 AM
the thing i did wrong was to compare it whit the value not whit the key, now i compare the search input with "key" and then i get the position of the variable in the table.
smurfofiel #5
Posted 04 October 2013 - 04:51 PM
You were close with your loop that you provided, what you've done wrong is that v is actually the value in the table, k is the key, therefore you want to do the following
 for k,v in pairs(uTable) do if user == uTable[k] then print(k) break end end 
However, what's the point of looking up the value again, when we already have it, so instead we can compare against v.
 for k,v in pairs(uTable) do if user == v then print(k) break end end 
Also you would want to actually store the index for later, something like so
 local userIdx for k,v in pairs(uTable) do if user == v then userIdx = k break end end --# you now know out here which index they were from the userIdx variable 
Lastly I have a feeling this is going to be used as some kind of login system, and that you're using the position the user is in the table to retrieve their password from another table? Yes? If so then I suggest doing the following change to your table layout/structure, its much simpler and requires no loops meaning that it is far more efficient.
 local uTable = { ["user1"] = "user password", ["user2"] = "qwerty", ["user3"] = "S3cure Pa55w0rD } 
The reason that the above table structure is so much better is that we can quickly and easily perform lookups on the table, like so
 local user = "user1" local pass = uTable[user] 
In the above code the pass variable will either contain the user's password, or if that user didn't exist it will be nil. Expending upon the above method can make for some very powerful, extensible, and efficient login systems (all be it not very secure having hardcoded usernames/passwords in the table, but that's a different topic).
but if i want to do it your way like:
 local uTable = { ["user1"] = "user password", ["user2"] = "qwerty", ["user3"] = "S3cure Pa55w0rD } 
how can i check if a certain passwords matches a certain player name. for example i get a user name from a read event, and the username is "mike" and i have a table like: ["mike"] = "1234", how can i check that 1234 belongs to "mike"?
theoriginalbit #6
Posted 04 October 2013 - 08:01 PM
but if i want to do it your way like:
 local uTable = { ["user1"] = "user password", ["user2"] = "qwerty", ["user3"] = "S3cure Pa55w0rD } 
how can i check if a certain passwords matches a certain player name. for example i get a user name from a read event, and the username is "mike" and i have a table like: ["mike"] = "1234", how can i check that 1234 belongs to "mike"?
Like this

local uTable = {
  ["mike"] = "1234",
}

local user = read()
local pass = read()

if uTable[user] == pass then
  print("The password was correct")
else
  print("The username or password is incorrect")
end
So in the above code doing uTable[user] will give us back the value (password) of the entered in username, meaning we can directly compare that against the password entered for a match
smurfofiel #7
Posted 05 October 2013 - 04:08 AM
aah i see thank you very much