Do While Loop Issue

I am trying to perform calculations for each year, when a user enters a specific number of years. The do while loop info I’ve found for Lua doesn’t mention anything abut incrementing variable names as well. Can someone please look this over and let me know if it is possible, and if so what am I doing wrong? Thanks!

Sean

[code]
i = 1
ii = 1
fsti3 = 5

while i < fsti3 do

if ( i == 1 ) then
fstiYr[i]Rtn = fsti1 + fstiYrConWI
else
fstiYr[i]Rtn = fsti1 + fstiYrConWI + fstiYr[i - 1]Rtn
end

fstiYr[i]LsExp = fstiYr[i]Rtn - 300

while ii < fsti3 do
local fstiYr[i]_h = display.newText( "Year " … [i], 0, 0, native.systemFont, 16 )
local fstiYr[i]RtnTxt = display.newText( “Return: $” … fstiYr[i]Rtn, 0, 0, native.systemFont, 14 )
local fstiYr[i]LsExpTxt = display.newText( “Less Expenses: $” … fstiYr[i]LsExp, 0, 0, native.systemFont, 14 )

if ( i == 1 ) then
fstiYr[i]_h.x = _w / 2; fstiYr[i]_h.y = backBtn.y + 35
else
fstiYr[i]_h.x = _w / 2; fstiYr[i]_h.y = fstiYr[i-1]RtnTxt.y + 25
end

fstiYr[i]RtnTxt.x = _w / 3; fstiYr[i]RtnTxt.y = fstiYr[i]_h.y + 25
fstiYr[i]LsExpTxt.x = _w / 1.5; fstiYr[i]LsExpTxt.y = fstiYr[i]RtnTxt.y + 25
ii = ii + 1
end
i = i + 1
end
[/code] [import]uid: 157382 topic_id: 30435 reply_id: 330435[/import]

Maybe it’s just me but it’s hard to to tell a) what’s going on with the above code and b) where the problem is in the code. if you can provide a simpler example with what you are trying to acheieve it would probably help. [import]uid: 147305 topic_id: 30435 reply_id: 121924[/import]

while i \< incNumber do  
  
 while ii \< incNumber do  
 local var[i]\_a = display.newText( "Year " .. [i], 0, 0, native.systemFont, 16 )  
  
 if ( i == 1 ) then  
 var[i]\_a.x = \_w / 2; var[i]\_a.y = var\_b.y + 35  
 else  
 var[i]\_a.x = \_w / 2; var[i]\_a.y = var[i-1]\_a.y + 25  
 end  
  
 ii = ii + 1  
 end  
 i = i + 1  
end  

Basically, I need a do while loop to increment a variable name for text output on the screen. I have some calculations going in a previous module, and want to output the text incrementally for a number the user inputs. Keeps giving me an error related to the square brackets on the first line. Is the syntax that I am using right? Does the variable name have to end with the [i], or can it be in the middle?

Sean [import]uid: 157382 topic_id: 30435 reply_id: 121948[/import]

I’m not aware of a language out there that lets you increment variables in that fashion. I’m not saying there isn’t but it at least isn’t a common practice. What you want to do is use a table to hold your text objects. You can then easily reference which object you are looking for by it’s index.

Below is kind of how to do it using your simple example.

[lua]local var_a = {}
while i < incNumber do

while ii < incNumber do
var_a[i] = display.newText( "Year " … [i], 0, 0, native.systemFont, 16 )

if ( i == 1 ) then
var_a[i].x = _w / 2; var_a[i].y = var_b.y + 35
else
var_a[i].x = _w / 2; var_a[i].y = var_a[i-1].y + 25
end

ii = ii + 1
end
i = i + 1
end[/lua] [import]uid: 147305 topic_id: 30435 reply_id: 121992[/import]

Maybe it’s just me but it’s hard to to tell a) what’s going on with the above code and b) where the problem is in the code. if you can provide a simpler example with what you are trying to acheieve it would probably help. [import]uid: 147305 topic_id: 30435 reply_id: 121924[/import]

while i \< incNumber do  
  
 while ii \< incNumber do  
 local var[i]\_a = display.newText( "Year " .. [i], 0, 0, native.systemFont, 16 )  
  
 if ( i == 1 ) then  
 var[i]\_a.x = \_w / 2; var[i]\_a.y = var\_b.y + 35  
 else  
 var[i]\_a.x = \_w / 2; var[i]\_a.y = var[i-1]\_a.y + 25  
 end  
  
 ii = ii + 1  
 end  
 i = i + 1  
end  

Basically, I need a do while loop to increment a variable name for text output on the screen. I have some calculations going in a previous module, and want to output the text incrementally for a number the user inputs. Keeps giving me an error related to the square brackets on the first line. Is the syntax that I am using right? Does the variable name have to end with the [i], or can it be in the middle?

Sean [import]uid: 157382 topic_id: 30435 reply_id: 121948[/import]

I’m not aware of a language out there that lets you increment variables in that fashion. I’m not saying there isn’t but it at least isn’t a common practice. What you want to do is use a table to hold your text objects. You can then easily reference which object you are looking for by it’s index.

Below is kind of how to do it using your simple example.

[lua]local var_a = {}
while i < incNumber do

while ii < incNumber do
var_a[i] = display.newText( "Year " … [i], 0, 0, native.systemFont, 16 )

if ( i == 1 ) then
var_a[i].x = _w / 2; var_a[i].y = var_b.y + 35
else
var_a[i].x = _w / 2; var_a[i].y = var_a[i-1].y + 25
end

ii = ii + 1
end
i = i + 1
end[/lua] [import]uid: 147305 topic_id: 30435 reply_id: 121992[/import]