04-19-2010, 04:09 PM
I didn't understand why a piece of my program didn't work as expected. I suspected something was wrong with the for-next loop. I tried some tests, simplified versions of the problem, and finally find out the following:
It was a surprise. The second loop prints the same original text every time.
I tried the equivalent code in Sinclair Basic, and of course it worked as expected:
Then I tried it without the loop:
And the problem remained.
Then I hardcoded some slicing values:
And it worked fine.
Is there something I'm missing in my code or is it a compiler issue?
Code:
dim text as string
dim i as ubyte
let text="hello world!"
cls
print "Fine:"
for i = 0 to len(text)-1
print i,text(to i)
next i
pause 0
cls
print "All texts are complete!:"
for i = 0 to len(text)-1
print i,;
let text = text(to len(text)-1)
print text
next i
stop
I tried the equivalent code in Sinclair Basic, and of course it worked as expected:
Code:
10 LET A$="hello world!"
20 FOR I=0 TO LEN(A$)-1
30 PRINT I,;
40 LET A$=A$(TO LEN(A$)-1)
50 PRINT A$
60 NEXT I
70 STOP
Then I tried it without the loop:
Code:
dim text as string
let text="hello world!"
cls
print text
let text = text(to len(text)-1)
print text
let text = text(to len(text)-1)
print text
let text = text(to len(text)-1)
print text
let text = text(to len(text)-1)
print text
let text = text(to len(text)-1)
print text
let text = text(to len(text)-1)
print text
let text = text(to len(text)-1)
print text
stop
Then I hardcoded some slicing values:
Code:
dim text as string
let text="hello world!"
print text
let text = text(to 10)
print text
let text = text(to 9)
print text
let text = text(to 8)
print text
let text = text(to 7)
print text
let text = text(to 6)
print text
let text = text(to 5)
print text
stop
Is there something I'm missing in my code or is it a compiler issue?
Marcos Cruz
http://programandala.net
http://programandala.net