Author Topic: mid statement  (Read 3276 times)

0 Members and 1 Guest are viewing this topic.

  • Guest
mid statement
« on: October 04, 2018, 06:59:33 PM »
Is there a mid statement? Apparently not, because this does not compile.

Code: [Select]
DIM s as string = "1234567890"
mid(s, 2, 3) = "xxx"
print s

Found this:

Code: [Select]
DIM s as string = "1234567890"
mid s, 2, "x"
print s

Weird syntax and apparently limited to one character.
« Last Edit: October 04, 2018, 07:06:40 PM by José Roca »

JRS

  • Guest
Re: mid statement
« Reply #1 on: October 04, 2018, 07:04:02 PM »
You're trying to make an assignment to a function.

José Roca

  • Guest
Re: mid statement
« Reply #2 on: October 04, 2018, 07:07:43 PM »
I'm trying to know if, besides the mid function, there is a mid statement.

Aurel

  • Guest
Re: mid statement
« Reply #3 on: October 04, 2018, 10:11:13 PM »
mid statement ?
what is that ...replace string
or replace substring with given length ?

JRS

  • Guest
Re: mid statement
« Reply #4 on: October 04, 2018, 10:26:16 PM »
In SB, MID is used to extract a sub-string. REPLACE() is used to modify an existing string.

I would rewrite your MID assignment like so.

Code: Script BASIC
  1. s = LEFT(s, 1) & "xxx" & MID(s, 2)
  2.  

« Last Edit: October 04, 2018, 10:54:42 PM by John »

Charles Pegge

  • Guest
Re: mid statement
« Reply #5 on: October 05, 2018, 12:28:03 AM »
José,

Yes, and the mid statement can take 2 forms:

mid(s, 2) = "xxx" 'pseudo assignment

mid(s, 2,"xxx") 'underlying form

Code: [Select]
DIM s as string = "1234567890"
mid(s, 2) = "xxx"
print s
mid(s, 2,"xxx")
print s


Aurel

  • Guest
Re: mid statement
« Reply #6 on: October 05, 2018, 04:51:34 AM »
Quote
mid(s, 2) = "xxx" 'pseudo assignment
:o
what i don't know for that

  • Guest
Re: mid statement
« Reply #7 on: October 05, 2018, 05:46:42 AM »
José,

Yes, and the mid statement can take 2 forms:

mid(s, 2) = "xxx" 'pseudo assignment

mid(s, 2,"xxx") 'underlying form

Code: [Select]
DIM s as string = "1234567890"
mid(s, 2) = "xxx"
print s
mid(s, 2,"xxx")
print s

Thanks, Charles. Works like with other dialects, although it is missing the second form, that optionally indicates the number of charcters to replace: Mid( text, start, length ) = expression. I use that second form very often because it ensures that only the wanted number of characters are replaced. Without it, I will have to check first if the replacement string has the appropriate length.

Code: [Select]
s = LEFT(s, 1) & "xxx" & MID(s, 2)

This does not do what I intend, besides being very inefficient.

I use the MID statement (no the MID function) very often because it is very efficient since it does not create temporary strings and does not use concatenations.


José Roca

  • Guest
Re: mid statement
« Reply #8 on: October 05, 2018, 06:16:28 AM »
Quote
mid(s, 2) = "xxx" 'pseudo assignment
:o
what i don't know for that

It is never too late to learn something new. Its use dates back to immemorial times.

JRS

  • Guest
Re: mid statement
« Reply #9 on: October 05, 2018, 08:15:42 AM »
What would this do?

MID(s,2,2) = "xxx"

José Roca

  • Guest
Re: mid statement
« Reply #10 on: October 05, 2018, 09:49:42 AM »
It is

MID(s,2,3) = "xxx"

What would this do?

To change the contents of the second, third and fourth characters of the string to "xxx".

s is the string, 2 the starting position, 3 the number of characters to change and "xxx" the characters to assign (they must be the same length that the specified number of characters to replace; if it is longer, only the first three will be taken). The string does not shrink or grow. It is a way to do a fast replacement without using pointers.

It has been a Basic's intrinsic statement for more time that I can remember.

JRS

  • Guest
Re: mid statement
« Reply #11 on: October 05, 2018, 10:00:58 AM »
I think you answered my question.

It will use as many characters from the assignment string specified in the MID statement and disregard the rest. Correct?

  • Guest
Re: mid statement
« Reply #12 on: October 05, 2018, 10:13:09 AM »
It depends on the implementation. O2 does not accept the third parameter and assumes that the number of characters to replace are equal to the length of the replacement string.

Therefore, if you use

MID(s,2) = "xxx"

it will replace three characters, starting at the second position.

and if you use

MID(s,2) = "wxyz"

it will replace four.

RTFM :)
https://github.com/JoseRoca/WinPBX/blob/master/docs/Oxygen/Intrinsic%20Functions.md#mid2
« Last Edit: October 05, 2018, 10:20:10 AM by José Roca »

JRS

  • Guest
Re: mid statement
« Reply #13 on: October 05, 2018, 10:34:08 AM »
I think Charles needs to add the 3rd argument to MID and it control how many of the characters used from the assignment string. Allowing both methods would be great.

Charles Pegge

  • Guest
Re: mid statement
« Reply #14 on: October 05, 2018, 08:06:15 PM »
Yes, I can incorporate that format. But I would like to know whether the last (source string) parameter of mid in your code, José, is usually a native string, or an asciiz string? There is an issue of optimisation.