table & for loop

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
NoAim91
Prole
Posts: 38
Joined: Mon Feb 20, 2017 10:28 am
Location: Germany

table & for loop

Post 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!
Zireael
Party member
Posts: 139
Joined: Fri Sep 02, 2016 10:52 am

Re: table & for loop

Post 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
MrFariator
Party member
Posts: 509
Joined: Wed Oct 05, 2016 11:53 am

Re: table & for loop

Post 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.
NoAim91
Prole
Posts: 38
Joined: Mon Feb 20, 2017 10:28 am
Location: Germany

Re: table & for loop

Post by NoAim91 »

thank you both :-)
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: table & for loop

Post 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
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
User avatar
darkmetalic
Prole
Posts: 17
Joined: Tue Feb 07, 2017 4:09 pm
Contact:

Re: table & for loop

Post 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
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 50 guests