Page 1 of 2

Mid(MyString$, Offset, Characters)

Posted: Thu May 04, 2006 3:39 pm
by RichardL
Some other BASICs I have used allow you to select all characters to the right of a position in a string using the syntax:

RParts$ = mid(MyString$, Offset)

PB always requires the three paramaters nomally associated with Mid(), so I need to write:

RPart$ = Mid(MyString$, N, Len(MyString$)-N+1) ; Messy

or:

RParts$ = Mid(MyString$, N, BigNumber) ; Works but not 'clean'

where I am sure that BigNumber is longer than the balance of the string.

Please could PB support the two parameter version of the call?

Posted: Thu May 04, 2006 4:26 pm
by Bonne_den_kule
Heard about Left() and Right()?
lol... :lol:

Posted: Thu May 04, 2006 4:37 pm
by Paul
How does Left() or Right() help here?
Left() starts at the beginning of the string and Right() starts from the end.
Mid() is the only one that allows for a starting position within the string.

I agree it would be nice to have the string length in Mid() as an option.
If no length is specified then length to end of string is used.

Often I use:
Mid(String$,pos,Len(String$))

Since PB seems to stop when the end of the string is reached, and this guarantees your length will exceed what is remaining in your string. ;)

Posted: Thu May 04, 2006 4:40 pm
by RichardL
Yes, of course! :D

(I have been programming with various BASICS for about 25 years.)

If I need all of a string to the right of a particular position, and the string does not have a fixed length Right() does not help.

Mid(String$, Start, Length) gves me what I want, but I have to calculate the value of Length.

PB stores strings with NULL terminators so the additional logic in MID() to (a) Detect no third parameter and set a flag then (2) Maka string of everything from 'Start' to the NULL into the returned value probably takes about as much work as writing this note!

Posted: Thu May 04, 2006 4:55 pm
by RichardL
Hi Paul,

Yes, you do much the same as I do. Using Len(String) is safe in PB, but I think its 'untidy'. :)

Richard

Posted: Thu May 04, 2006 5:52 pm
by Straker
RichardL wrote:(I have been programming with various BASICS for about 25 years.)
Rule 1: Don't assume PureBasic is a BASIC dialect.

Posted: Thu May 04, 2006 6:01 pm
by thefool
I think its a good idea. I have needed this a few times only, however it would definently be more clean this way.

Posted: Thu May 04, 2006 6:57 pm
by einander
I'm using this:

Code: Select all

Macro Mids(A,n)
   Mid(A,n,Len(A))
EndMacro 
  
b$="abcdefgh"
Debug Mids(b$,2)

Repeat 
Until WaitWindowEvent()=#PB_Event_CloseWindow  
 

Posted: Thu May 04, 2006 7:00 pm
by Trond
Paul wrote:How does Left() or Right() help here?

Code: Select all

Macro MidInf(String, StartPos)
  Right(String, Len(String)+1-StartPos)
EndMacro
Edit: I'm too slow.

Posted: Thu May 04, 2006 7:09 pm
by einander
Trond:
Your macro is clever but slower.

Code: Select all

Macro MidInf(String, StartPos)
  Right(String, Len(String)+1-StartPos)
EndMacro
 
Macro Mids(A,n)
   Mid(A,n,Len(A))
EndMacro 
  
b$="abcdefgh"

ti=GetTickCount_()
For i=0 To 100000
A$= Mids(b$,2)
Next
Debug GetTickCount_()-ti

ti=GetTickCount_()
For i=0 To 100000
  A$= MidInf(b$,2)
Next
Debug GetTickCount_()-ti

Repeat 
Until WaitWindowEvent()=#PB_Event_CloseWindow  
 

Posted: Thu May 04, 2006 7:56 pm
by Psychophanta
Faster:

Code: Select all

Macro Mids(A,n)
  PeekS(@A+n-1)
EndMacro
:twisted:

Posted: Thu May 04, 2006 8:02 pm
by Trond
NEVER EVER NEVER EVER NEVER EVER enable the debugger when you're doing speed tests. Turn off the debugger and mine is faster.

But I didn't realize this was a speed competition. This beats everything so far:

Code: Select all

Macro MidInf2(String, StartingPos)
   PeekS(@String + SizeOf(Character)*(StartingPos-1))
EndMacro

Posted: Thu May 04, 2006 8:03 pm
by netmaestro
even faster (macro is redundant)

Peeks(@string$+ n)

Posted: Thu May 04, 2006 8:03 pm
by Psychophanta
Trond wrote:NEVER EVER NEVER EVER NEVER EVER enable the debugger when you're doing speed tests. Turn off the debugger and mine is faster.

But I didn't realize this was a speed competition. This beats everything so far:

Code: Select all

Macro MidInf2(String, StartingPos)
   PeekS(@String + SizeOf(Character)*(StartingPos-1))
EndMacro
:lol: speed war strikes again

Posted: Thu May 04, 2006 8:05 pm
by Trond
netmaestro wrote:even faster (macro is redundant)

Peeks(@string$, n)
But far from the same functionality :wink:

EVEN faster:

Code: Select all

Z + 1 - 3