Jump to content




variable inside "for" loop

help

  • You cannot reply to this topic
11 replies to this topic

#1 Wing

  • Members
  • 58 posts

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!

#2 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 04 February 2013 - 01:53 PM

Topic moved. Please post questions here in Ask a Pro next time.

#3 Wing

  • Members
  • 58 posts

Posted 04 February 2013 - 02:14 PM

Where'd I put it? lol, not like I'm new to asking questions :D

#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 04 February 2013 - 02:27 PM

above the for loop put
tFound = {}


#5 Wing

  • Members
  • 58 posts

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)

#6 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 04 February 2013 - 03:23 PM

View PostWing, on 04 February 2013 - 02:14 PM, said:

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.

#7 Wing

  • Members
  • 58 posts

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)

#8 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

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.

#9 Wing

  • Members
  • 58 posts

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()


#10 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

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.

#11 Wing

  • Members
  • 58 posts

Posted 07 February 2013 - 11:31 AM

Trying to learn lulz, also started doing this after work at 1am about 3 days ago :P

#12 Wing

  • Members
  • 58 posts

Posted 07 February 2013 - 12:22 PM

That "count" in the wrong place screwed it all, thanks for pointing that out!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users