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

variable inside "for" loop

Started by Wing, 04 February 2013 - 12:51 PM
Wing #1
Posted 04 February 2013 - 01:51 PM
I need this variable else where, although it's not working outside the "for" loop!

for name, basicDetails in pairs(targets) do
local count = count+1
print(name)
if (tostring(name) ~= "IWinGI" or "NateFlax" or "Joseph3262" or "101Alf101") then
  local moreDetails = prox.getTargetDetails(name)
  table.insert(tFound, name)
  tFound[count] = {}
  table.insert(tFound[count], moreDetails.Position["X"])
  table.insert(tFound[count], moreDetails.Position["Y"])
  table.insert(tFound[count], moreDetails.Position["Z"])
  end
end
print(tFound[count][1])
The last line just prints an empty line….
But if you put the print line inside the loop, it will print the value!
Cranium #2
Posted 04 February 2013 - 01:53 PM
Topic moved. Please post questions here in Ask a Pro next time.
Wing #3
Posted 04 February 2013 - 02:14 PM
Where'd I put it? lol, not like I'm new to asking questions :D/>
theoriginalbit #4
Posted 04 February 2013 - 02:27 PM
above the for loop put

tFound = {}
Wing #5
Posted 04 February 2013 - 03:08 PM

function getTarget()
local targets = prox.getTargets()
local tFound = {}
local count = 1
for name, basicDetails in pairs(targets) do
local count = count+1
print(name)
if (tostring(name) ~= "IWinGI" or "NateFlax" or "Joseph3262" or "101Alf101") then
local moreDetails = prox.getTargetDetails(name)
table.insert(tFound, name)
tFound[count] = {}
table.insert(tFound[count], moreDetails.Position["X"])
table.insert(tFound[count], moreDetails.Position["Y"])
table.insert(tFound[count], moreDetails.Position["Z"])
end
end
print(tFound[count][1])
That was the full code
(not complete, there is more below)
Cranium #6
Posted 04 February 2013 - 03:23 PM
Where'd I put it? lol, not like I'm new to asking questions :D/>
You had posted it in Tutorials. It's ok as long as you don't make it a habit.
Wing #7
Posted 04 February 2013 - 03:27 PM
I guess at the time I was like, "I need a tutorial to figure out my problem!"
Then, accidentally in tutorials, derp. (herp sentence intended)
Lyqyd #8
Posted 07 February 2013 - 10:40 AM
Remove the local from the line that increments count inside the loop. Keep the local on the line that declares it outside the loop.
Wing #9
Posted 07 February 2013 - 10:47 AM
That didn't fix it…. Error happens at line 36, Table expected, got string. btw, x,y,z variables don't get anything, although if tested inside the loop, they do contain information, but not outside!
This is the entire code,

prox = sensor.wrap("right")
function getPos()
local cX,cY,cZ = mt.getPos()
end
function getTarget()
local targets = prox.getTargets()
tFound = {}
count = 1
for name, basicDetails in pairs(targets) do
count = count+1
print(name)
if (tostring(name) ~= "IWinGI" or "NateFlax" or "Joseph3262" or "101Alf101") then
  local moreDetails = prox.getTargetDetails(name)
  table.insert(tFound, name)
  tFound[count] = {}
  table.insert(tFound[count], moreDetails.Position["X"])
  table.insert(tFound[count], moreDetails.Position["Y"])
  table.insert(tFound[count], moreDetails.Position["Z"])
  end
end
print(tFound[count][1])
count = 1
repeat
x = tFound[count][1]
y = tFound[count][2]
z = tFound[count][3]
newvector = vector.new(x,y,z)
vti = newvector:length()
table.insert(tFound[count], vti)
count = count +1
until (tFound[count] == nil)
count = 1
repeat
print(tFound[count][4])
count = count +1
until (tFound[count] == nil)
end
getTarget()
Lyqyd #10
Posted 07 February 2013 - 11:10 AM
It certainly did fix the stated problem; there's simply far more wrong with your code than that. You also didn't even implement the fix correctly. Why are you inserting the name into the targets table? You end up overwriting things in a crazy manner. Have you rubber ducked through the logic of this at all? For example,

Declare new empty table.
Count is 1.
Enter loop, add one to count.
Count is 2.
Insert name of target into empty table.
Table contains 1: name of object.
Set index 2 (count is 2) to coordinates of that target.
Repeat through all targets, inserting name at the end and immediately overwriting it with that target's coordinates.
Set count back to 1.
Iterate through table, expecting each index to contain a table, which all but the first now do. (The first contains the string name of the first target).

Let's just say you should be using a lot less of table.insert and a lot more iterative for loops. It also seems that you may not quite grasp table operations yet, so it may help to read up on those sections of the reference manual or the PIL docs.
Wing #11
Posted 07 February 2013 - 11:31 AM
Trying to learn lulz, also started doing this after work at 1am about 3 days ago :P/>
Wing #12
Posted 07 February 2013 - 12:22 PM
That "count" in the wrong place screwed it all, thanks for pointing that out!