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

pairs() command question

Started by randomsteve95370, 19 October 2013 - 02:56 PM
randomsteve95370 #1
Posted 19 October 2013 - 04:56 PM
I have a question about the pairs() command,

I would like to be able to run something like this.

print("What program would you like me to run?")
X = read()
print("Who shall I tell to run ", X)
Y = read()
for x,y in pairs(Y) do
	  rednet.send(y, X)

Obviously this isnt a working piece of code as I would need to set the tables, open rednet, ect ect. But my problem is that I can seem to get the pairs() function to use a variable such as pairs(X). Is there a way to set a variable and then use the use the pairs command to take that variable and run the correct table?
Lyqyd #2
Posted 19 October 2013 - 09:07 PM
Split into new topic.
Bomb Bloke #3
Posted 19 October 2013 - 09:38 PM
There seems to be no need for a table here, and since you aren't defining X or Y as such, no need for pairs() either.

Anyway, read() returns strings, but you want the computer ID to be in the form of a numeral:

print("What program would you like me to run?")
X = read()
print("Who shall I tell to run "..X)
Y = tonumber(read())
rednet.send(Y, X)
randomsteve95370 #4
Posted 19 October 2013 - 09:57 PM
Disclamer. All code inside of this post is for a demonstrative purpose

and thus does not, nor will not work unless

properly formated and rewritten.



My above code was just for example purposes. As I noted that is not complete code, I would need to define a table, have different functions, send the rednet to another computer with its own program ect ect. What I can't figure out is this.

Lets say I have 10 Wireless turtles and I have them split into groups. Lets say: 4 Miners, 4 builders, and 2 fuelers.

I then run a bit of script inside of a program that does something like this


rednet.broadcast("checkIn")
   while gotMsg do
	  id,msg,dist = rednet.receive(1)
	  if msg == "Miner" then
		 print(id..":"..msg)
		 miners[#Miners+1] = id
	  elseif msg == "Builder" then
		 print(id..":"..msg)
		 loaders[#Builders+1] = id
	  elseif msg == "Fuelers" then
		 print(id..":"..msg)
		 fuelBosses[#Fuelers+1] = id
		 miners[#miners+1] = id
	  elseif msg == "Done" then
	  else
		 print("Done")
		 gotMsg = false

The 3 sets of turtles would be programmed to return the correct message depending on what they are. Make sense? Ok good.

Now lets say that I want to be able to control them from the main computer. I want the computer to ask the use what program to run and who to tell to run it.

Like so:

print("What program would you like me to run?")
var1= read()
print("Who shall I tell to run ", var1)
var2 = read()
for x,y in pairs(var2) do
		  rednet.send(y, var1)

So that piece of code would get what program you want to run and store it in var1 and then get what turtles you want to run and store it in var2. Everything works fine accept the fact that it seems that you can't use a variable to define what table you want to call in the pairs() function.

So my question is. Is there a way to use a variable to define the table in the pairs() function?
Bomb Bloke #5
Posted 19 October 2013 - 10:21 PM
Probably, but I still see no need to use pairs(). Try playing around with this code:

turtles = { Miners={12,45,23}, Diggers={56,74,26}, Fuelers={18,43,34} }

target = "Diggers"

for i=1,#turtles[target] do print(turtles[target][i]) end
randomsteve95370 #6
Posted 19 October 2013 - 10:41 PM
I have no idea what that code above does, for me to play around with it you will have to explain how I am suppose to use it. Honestly though I dont need to know how to rewrite my demo code, just if there is a way to use a variable to define the name of a table inside of the parentheses in Pairs() At this point my code of the 4 programs is nearly a thousand lines long and would require several dozen functions rewritten and a lot of time spent just redefining all of my tables as you have up above.
Lyqyd #7
Posted 19 October 2013 - 11:43 PM
You really need to post your whole code. Showing tiny little snippets is not at all helpful, especially since none of the code you've posted so far has any tables in it.
Bomb Bloke #8
Posted 19 October 2013 - 11:49 PM
I have no idea what that code above does, for me to play around with it you will have to explain how I am suppose to use it.

Let me rephrase: If you want to know what it does, paste it into a new script and run it.

Honestly though I dont need to know how to rewrite my demo code, just if there is a way to use a variable to define the name of a table inside of the parentheses in Pairs() At this point my code of the 4 programs is nearly a thousand lines long and would require several dozen functions rewritten and a lot of time spent just redefining all of my tables as you have up above.

Essentially, you want to refer to a variable using a string.

Using table keys per my example gives you a simple method of doing so. Yes, you'd need to change your table references somewhat (eg, miners[#Miners+1] would become something like turtles["Miners"][#turtles["Miners"]+1]).

If you don't like that method, then you should be able to pull the lookup using for x,y in pairs(getfenv()[var2]) do so long as all your tables are global. I've no idea how you'd do it if they're properly localised; you'd want to research getfenv() further in that case.