Oxygen Basic
Programming => Bugs & Feature Requests => Topic started by: JRS on March 23, 2014, 11:32:12 AM
-
Charles,
I don't understand why this doesn't work. (compiles fine) I also tried it with a & operator to concatenate the string. (no luck)
dim sys x
dim string revtext
for x = 11 to 1
revtext = revtext + mid("Hello World",x,1)
next
print revtext
-
Hi John,
step -1 is all that is required
-
Must be getting tired. :(
The STEP -1 is also required in SB to work. Knowing Peter, I would have assumed this being obvious, the STEP -1 wouldn't have been required.
revtext = ""
for x = 11 to 1 step -1
revtext &= mid("Hello World",x,1)
next
print revtext,"\n"
jrs@laptop:~/sb/sb22/test$ scriba formid.sb
dlroW olleH
jrs@laptop:~/sb/sb22/test$
-
Yes, Oxygen does not evaluate the range during compilation. The end-point is usually specified by a variable.
Here is a high-performance equivalent, using bytes, like C
string text="Hello World"
sys le=len text
string revtext=nuls le
byte b at (strptr text)
byte r at (le-1+strptr revtext)
sys x
for x = 1 to le
r=b
@b++
@r--
next
print revtext
-
revtext = ""
for x = -12 to -1
revtext &= mid("Hello World",NOT(x),1)
next
print revtext,"\n"
8)
-
Yes, I forgot about negative mid offsets:
revtext=""
for x = 1 to 11
revtext &= mid("Hello World",-x,1)
next
print revtext
-
Sweet!
Works in both O2 and SB.
The second argument specifies the start position of the resulting substring in the original string x; and the last argument specifies the number of characters to take from the original string x. If the third argument is missing the substring lasts from the start position to the end of the string. If the second argument is not defined the start of the substring is at the start of the original string. In other words if the second argument is missing it is the same as value 1. If the second argument is zero or negative it will specify the start position counting the characters from the end of the string.
revtext=""
for x = 1 to 11
revtext &= mid("Hello World",-x,1)
next
print revtext,"\n"
jrs@laptop:~/sb/sb22/test$ scriba negmid.sb
dlroW olleH
jrs@laptop:~/sb/sb22/test$