29 posts
Posted 04 April 2014 - 12:52 AM
Just working on tables I took a small example from direwolf even though I'm also reading and learning on my own but I saw this in a video. I'm trying to learn how to set the K and V? In his example the (K,V) are called (num,name) but I don't see anywhere where he sets the value to num or name. I know that the player name should be my key and true should be the value but how do I actually declare this? Can anyone please be a little elaborate on how all this is supposed to work? Also as a reference to kind of what I'm talking about…
https://www.youtube.com/watch?v=3LK9d5yMgS4&list=UU_ViSsVg_3JUDyLS3E2Un5g … Right around 14:24. Thank you so much in advance!
p = peripheral.wrap("top")
local Allowed = {}
Allowed["chardo440"] = true
while true do
player = p.getPlayerNames()
for k,v in pairs(player) do
print(k,v)
sleep(2)
end
end
115 posts
Posted 04 April 2014 - 12:55 AM
Heres a quote from a very similar question
example_table = {
1 = "hello",
2 = "oh yea",
3 = "bananna",
foo = 7,
bar = false,
}
for key, value in pairs(example_table) do
print("Key: "..k.." Value: "..v)
end
Result:
Key: 1 Value: hello
Key: 2 Value: oh yea
Key: 3 Value: bananna
Key: foo Value: 7
Key: bar Value: false
Also:
http://lua-users.org/wiki/ForTutorial
This should hopefully help you
Edited on 03 April 2014 - 10:56 PM
29 posts
Posted 04 April 2014 - 01:07 AM
I do understand that part. Thank you that's off of lua-users right? However I fixed my code to now…
print(k, tostring(v))
Which atleast successfully prints. However, it prints –1chardo440–. Shouldn't it print –chardo440 true–? I just don't understand why that works?
edit - just went back to print k,v it's getting the same output as 1chardo440
Edited on 03 April 2014 - 11:14 PM
31 posts
Posted 04 April 2014 - 01:18 AM
what you are telling it to print is the table's key, and the tables value for each entry. if you read this page it should help you out in learning what and why its pulling it.
http://computercraft.info/wiki/Tables
29 posts
Posted 04 April 2014 - 02:05 AM
Yeah I know about that much but in my code it prints "chardo440" first so I have to do print(v,k) to get it to work in order for some reason? but still it's printing "chardo4401" instead of "chardo440 true". So it like reversed it to print the value first? I read the tables but I don't see about anything about what I'm talking about. I hate learning but I like it at the same time xD.
Edited on 04 April 2014 - 12:07 AM
31 posts
Posted 04 April 2014 - 02:19 AM
this might also help you, you have two tables allowed, and player (I am guessing from a scanner), you are doing k,v on player, which does not have your true statement thats in allowed. However for players, the first key in the table is 1, and then the value is your name
29 posts
Posted 04 April 2014 - 04:15 AM
this might also help you, you have two tables allowed, and player (I am guessing from a scanner), you are doing k,v on player, which does not have your true statement thats in allowed. However for players, the first key in the table is 1, and then the value is your name
Ohh ok I see what is happening now. So the sensor peripheral prints it's own table and it print's it like a standard key value table like
t = { [1] = "chardo440" , [2] = "Everyone else"} … essentially. Hopefully I'm on the right track :P/> Thanks for the help!
8543 posts
Posted 04 April 2014 - 04:41 AM
Uh, no the sensor peripheral doesn't print anything. Your code is printing out the table from the sensor peripheral. Read through it carefully. I took out the Allowed table definition, since it isn't used anywhere.
p = peripheral.wrap("top")
while true do
player = p.getPlayerNames()
for k,v in pairs(player) do
print(k,v)
sleep(2)
end
end
29 posts
Posted 04 April 2014 - 02:30 PM
Well the allowed table is for later use. It's just what I had in the bit of code so far. The sensor peripheral doesn't print the table type like I said? It looks like it does though it prints 1chardo440 or 2other person. I'm not home to try it out but I will test it out later to see if removing the allowed table did anything.
I see what your saying now the peripheral gets the names but the k,v in pairs put it in the table form. So since it doesn't save will it be 1chardo440 and 1anyone else? Like the table would constantly refresh making a new entry. Or does it auto add to the table?
Edited on 04 April 2014 - 12:36 PM
1281 posts
Posted 04 April 2014 - 02:39 PM
You're not getting this at all. The sensor returns a table. The pairs loop iterates through the table, returning each key and value pair as k,v because you defined it as such. You then use print to print these together for every iteration. k is actually 1 and v is actually chardo440, but when you pass the 2 variables to the same print it will print them together as "1chardo440". Do it like this instead, and the result should be somewhat clearer to you.
print("k: "..k.." v: "..v)
Edited on 04 April 2014 - 12:40 PM
29 posts
Posted 05 April 2014 - 01:06 AM
You're not getting this at all. The sensor returns a table. The pairs loop iterates through the table, returning each key and value pair as k,v because you defined it as such. You then use print to print these together for every iteration. k is actually 1 and v is actually chardo440, but when you pass the 2 variables to the same print it will print them together as "1chardo440". Do it like this instead, and the result should be somewhat clearer to you.
print("k: "..k.." v: "..v)
Yeah, I got that. I think I'm just wording it really stupid. It makes sense in my head but my words don't. I have the sensor working displaying everything and the system I built is working now :)/> Sorry for all the confusion. Everything is working now though. And I understand it much much clearer.