Author Topic: O2 MID  (Read 3029 times)

0 Members and 1 Guest are viewing this topic.

JRS

  • Guest
O2 MID
« 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)

Code: [Select]
dim sys x
dim string revtext

for x = 11 to 1
  revtext = revtext + mid("Hello World",x,1)
next

print revtext


Charles Pegge

  • Guest
Re: O2 MID
« Reply #1 on: March 23, 2014, 12:38:44 PM »
Hi John,

step -1 is all that is required

JRS

  • Guest
Re: O2 MID
« Reply #2 on: March 23, 2014, 12:48:42 PM »
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.

Code: [Select]
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$

« Last Edit: March 23, 2014, 12:55:34 PM by John »

Charles Pegge

  • Guest
Re: O2 MID
« Reply #3 on: March 23, 2014, 01:03:11 PM »
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


JRS

  • Guest
Re: O2 MID
« Reply #4 on: March 23, 2014, 04:27:19 PM »
Code: [Select]
revtext = ""

for x = -12 to -1
  revtext &= mid("Hello World",NOT(x),1)
next

print revtext,"\n"

 8)
« Last Edit: March 23, 2014, 04:38:03 PM by John »

Charles Pegge

  • Guest
Re: O2 MID
« Reply #5 on: March 23, 2014, 06:50:05 PM »
Yes, I forgot about negative mid offsets:


revtext=""

for x = 1 to 11
  revtext &= mid("Hello World",-x,1)
next

print revtext

JRS

  • Guest
Re: O2 MID
« Reply #6 on: March 23, 2014, 07:56:57 PM »
Sweet!

Works in both O2 and SB.

Quote from: SB MID user docs
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.

Code: [Select]
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$
« Last Edit: March 23, 2014, 08:50:15 PM by John »