Page 1 of 1

table & for loop

Posted: Thu Feb 23, 2017 11:26 am
by NoAim91
Hi, I have no clue why the following dosn´t work:

function createGraph()
for i=1, 60 do
graph = {}
graph = {id= i, x=40*i, y=65}
return graph
end
end

createGraph()

print(graph[1].x) --> print 40, all fine here
print(graph[2].x) --> gives me a error, because nil value :-( but why???

I like to create a table which get filled at the start of the program... very many thanks for help!

Re: table & for loop

Posted: Thu Feb 23, 2017 11:42 am
by Zireael
Your graph table only has one line because you're reinitializing it on every run of the loop
Move graph = {} two lines higher (so that it's before the for i=1,60)

Don't worry, it happened to me too :P

Re: table & for loop

Posted: Thu Feb 23, 2017 11:46 am
by MrFariator
Got ninja'd by Zireael but I'll post what I wrote anyway.

Your code has a few problems, so let's step through it:
  • Initialize the for loop, so far so good.
  • Take the global variable graph, and assign a table into it. However, because this assignment is done for every for loop iteration, the table gets overwritten and thus emptied.
  • Write a table with fields id, x and y to the variable graph. This will overwrite the empty table done during the previous step, and as such does it for every iteration.
  • You prematurely end the for loop by breaking out of it with the "return" keyword; which means that the for loop actually breaks during the first iteration. As such, even if above two points were fixed you'd only get a graph with length of one.
So the appropriate changes you should do are the following:

Code: Select all

function createGraph() 
  graph = {}
  for i=1, 60 do
    graph[i] = {id= i, x=40*i, y=65}
  end
  return graph -- Only return the graph once the for loop has finished
end

createGraph()
After that you can use graph[1].x, graph[2].id, or whatever other fields you need.

Re: table & for loop

Posted: Thu Feb 23, 2017 12:10 pm
by NoAim91
thank you both :-)

Re: table & for loop

Posted: Thu Feb 23, 2017 5:19 pm
by Positive07

Code: Select all

function createGraph() 
  local graph = {}
  for i=1, 60 do
    graph[i] = {id= i, x=40*i, y=65}
  end
  return graph -- Only return the graph once the for loop has finished
end

local graph = createGraph()

Make graph local otherwise you would be overriding a global variable

Re: table & for loop

Posted: Fri Feb 24, 2017 9:55 am
by darkmetalic
Loops are a number of repetitions inside a scope (do end repeat until), the loop can be broken using [break] (while true do break end), or it can terminate when you determine or be infinite (while true do end) and cause an infinite loop [crashing your project and bombarding your system memory] if you do not pay attention to what you're doing. . Placing local in your table is important because what will be useful to it is only the return that will be the table to a variable that will become this table, if not by local before it will be global from this function.

Code: Select all

function createGraph() 
local graph = {}
for i=1, 60 do
graph[i] = {id=i,x=40*i,y=65}
end
return graph
end

function createGraph() 
local graph,i = {},1
while i<=60 do
graph[i]={id=i,x=40*i,y=65}
i=i+1
end
return graph
end

function createGraph() 
local graph,i = {},1
repeat
graph[i]={id=i,x=40*i,y=65}
i=i+1
until i>60
return graph
end