Nested Loop and variables

The vtotal_sequence_count is resetting to 0 when the loop array element changes, I am expecting it to end up at 149.

What am I missing?

vparent_data = "Steve, Jim, Dave"
vparent_result = ""
vresult = ""
vtotal_sequence_count = 0
vcount = 1

looparray vparent_data, ",", element, index
    vcount = 1
	loop
		stoploopif vcount > 50

            vresult = vresult +cr()+ element + " - " +str(vtotal_sequence_count)

		vcount = vcount + 1
		vtotal_sequence_count = vtotal_sequence_count + 1

	endloop

   vparent_result = vparent_result + cr() + vresult
endloop

vcount is given an initial value of 50, and its first pass through the loop increases that to 51. After that its value is always 51 when it hits the stoploopif statement, so none of the rest of the code in the inner loop ever runs again. When it ends, vresult still has the value it was given on that first pass, when vtotal_ sequence_count had a value of 0. The value of vtotal_ sequence_count was actually increased to 1, but that value is never used.

Im sorry Dave, vCount should of been 1

It sounds like not only should it be 1, but the assignment to 1 should be after the looparray statement. Don’t you want to reset it back to 1 for each element of the vparent_data array?

Also, I suspect that

vresult = ""

should also be inside the looparray, not before it.

Yes, I changed my original post to reflect. Sorry, many interruptions while coding.

Yes Jim, you are correct. This code behaves as expected. Thank you.

let vparent_data = "Steve, Jim, Dave"
let vparent_result = ""
let vresult = ""
let vtotal_sequence_count = 0
let vcount = 1

looparray vparent_data, ",", element, index
    vcount = 1
    vresult = ""
	loop
		stoploopif vcount > 50

            vresult = vresult +cr()+ element + " - " +str(vtotal_sequence_count)

		vcount = vcount + 1
		vtotal_sequence_count = vtotal_sequence_count + 1

	endloop

   vparent_result = vparent_result + cr() + vresult
endloop

displaydata vparent_result

In its amended form, vtotal_ sequence_count now finishes at 150 (not 149) as you must have meant: starting at zero and incremented fifty times for each of the three elements of the array.

But why use

loop
	stoploopif vcount > 50

when you could write it thus:

loopwhile vcount ≤ 50

And why write

    vtotal_sequence_count = vtotal_sequence_count + 1

when you could have this:

    increment vtotal_sequence_count

Also, why add the optional index parameter to the looparray statement if you are not going to make use of the value of index within the loop?

And if the inner loop should always run fifty times, it’s much more efficient to replace

vcount = 1
loop
	stoploopif vcount > 50
	vresult = vresult +cr()+ element + " - " +str(vtotal_sequence_count)
	vcount = vcount + 1
	vtotal_sequence_count = vtotal_sequence_count + 1
endloop

with this:

for vcount, 1, 50
    vresult = vresult + cr() + element + " - " + str(vtotal_sequence_count)
    increment vtotal_sequence_count
endloop

And you appear to be using ", " as the separator where vparent_data is assigned, but then use "," in the looparray statement, thus the second and third values of element will have a leading space. Is that what you want?