The following warnings occurred:
Warning [2] Undefined array key 0 - Line: 1677 - File: showthread.php PHP 8.2.31 (Linux)
File Line Function
/inc/class_error.php 157 errorHandler->error
/showthread.php 1677 errorHandler->error_callback
/showthread.php 916 buildtree




Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
string slicing using len()
#1
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:

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
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:
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
And the problem remained.
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
And it worked fine.

Is there something I'm missing in my code or is it a compiler issue?
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)