MID strings

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
eevee
User
User
Posts: 69
Joined: Sat Jan 08, 2005 6:24 pm
Location: UK

MID strings

Post by eevee »

The Mid function only seems to work in one direction such as:-

Ans$ = Mid(afirst$, 3, 1)

It would be very useful if it followed normal convention in other basics and worked in reverse such as:-

Mid(afirst$, 3, 1) = "4"

Appreciate that ReplaceString is not an answer since this function seems to require that the character being replaced in the original string is known.

At the moment it would seem one has to deconstruct the string in order to insert a different character, or alternatively, find the character using the Mid function in order to identify the character one is replacing using ReplaceString!.
Regards,

Ernest
freedimension
Enthusiast
Enthusiast
Posts: 613
Joined: Tue May 06, 2003 2:50 pm
Location: Germany
Contact:

Re: MID strings

Post by freedimension »

eevee wrote: The Mid function only seems to work in one direction such as:-

Ans$ = Mid(afirst$, 3, 1)

It would be very useful if it followed normal convention in other basics and worked in reverse such as:-

Mid(afirst$, 3, 1) = "4"
I consider that dirty styled code. Why should I assign values to a function? Fred would have to make an exception only for this function, only to implement dirty code that causes the compiler to be bloatier, uglier and that could cause other bugs undreamt of. As to the convention: PureBasic <> BASIC (well, I know about the advertising :( ).

What's wrong about a command like

Code: Select all

result$ = InSet(original$, replacement$, position)
You could code that yourself or ask Fred to add it to the string library.
<°)))o><²³
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4789
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

opinions

Post by Fangbeast »

freedimension, this is the " Feature Requests and Wishlists" section where people may ask Fred for whatever they like without being told what they can and cannot have by an opinionated 3'rd party and Fred may or may not consider it but that is his perogative and not yours.. Evee didn't ask for your opinion of his request and this is the wrong section for it.

Regards.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

well, the question has been raised before, and though dirty it's regular basic syntax in most other basics :-)

fortunately this one is easy to write :-)

Code: Select all

Procedure.s x_mid(x.s,p.l,l.l,y.s)
  x=Left(x,p-1)+y+Mid(x,p+l,Len(x))
  ProcedureReturn x
EndProcedure

Debug x_mid("1234",2,2,"A")
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
PolyVector
Enthusiast
Enthusiast
Posts: 499
Joined: Wed Sep 17, 2003 9:17 pm
Location: Southern California
Contact:

Post by PolyVector »

@blueznl
I didn't notice your post and I came up with...the exact same thing... :)

Code: Select all

Procedure.s InSet(OriginalString$,Position,Length,ReplacementString$)
  ProcedureReturn Left(OriginalString$,Position-1)+ReplacementString$+Right(OriginalString$,Len(OriginalString$)-(Position+Length-1))
EndProcedure

String$="Hello, my name is PolyVector :)"
Debug InSet(String$,8,7,"your master")
freedimension
Enthusiast
Enthusiast
Posts: 613
Joined: Tue May 06, 2003 2:50 pm
Location: Germany
Contact:

Re: opinions

Post by freedimension »

Fangbeast wrote:freedimension, this is the " Feature Requests and Wishlists" section where people may ask Fred for whatever they like without being told what they can and cannot have by an opinionated 3'rd party and Fred may or may not consider it but that is his perogative and not yours.. Evee didn't ask for your opinion of his request and this is the wrong section for it.
Well, than consider my post as the wish NOT TO IMPLEMENT THIS

If you can't stand discussion what the h... are you doing in a discussion board?
If you can't see it, I wanted to help but now I'm really pissed off. Thx, you made my day.
<°)))o><²³
eevee
User
User
Posts: 69
Joined: Sat Jan 08, 2005 6:24 pm
Location: UK

Post by eevee »

Hey folks!

Stop throwing things at each other!

You are all obviously real programmers and not just code sloggers like me!

All I know is that the sheer simplicity of:-

Code: Select all

 Mid(afirst$, 3, 1) = "4" 
is not available.

I now have to use something as convoluted as:-

Code: Select all

afirst$=Left(afirst$,3-1)+"4"+Right(afirst$,Len(afirst$)-3)
or a construct based on:-

Use MID to detect existing character in the string
Use ReplaceString to do the replacement.

If the suggested InSet is the 'proper' way to do things, that' fine - it would still give me a single line of code that can be inserted anywhere in the program without much thought.

As it is, in the example above, one has to be careful to repeat the position (the two 3s) or set it as a Procedure (with the parameter-passing overheads that would incur) as others have indicated.

Now do you want a real battle? My pet dislike?? The Keyboard functions??? :)
Regards,

Ernest
PolyVector
Enthusiast
Enthusiast
Posts: 499
Joined: Wed Sep 17, 2003 9:17 pm
Location: Southern California
Contact:

Post by PolyVector »

@freedimention
Fangbeast is just bein' weird, your comments are always welcome... I totally agree that you can't change the syntax of an entire language for a single command... Every BASIC has it's own dialect and this one simply wouldn't fit...

@eevee
It's a good suggestion, but isn't likely to be practical to impliment... But that's up to Fred :)
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Post by GPI »

Btw, i would prefer the name MidSet(a$,pos.l,len.l,replace$,[flag])
Flag=
#PB_MidSet_Direct = change direct in a$, do not create a new string and return it. the length of replace$ must be len.l Like ReplaceString.
freedimension
Enthusiast
Enthusiast
Posts: 613
Joined: Tue May 06, 2003 2:50 pm
Location: Germany
Contact:

Post by freedimension »

GPI wrote:Btw, i would prefer the name MidSet(a$,pos.l,len.l,replace$,[flag])
Flag=
#PB_MidSet_Direct = change direct in a$, do not create a new string and return it. the length of replace$ must be len.l Like ReplaceString.
Very good ideas.
One Question though: What is the use of the additional len.l parameter? Isn't the length given by the length of replace$ ? I'm (really) sure you thought something about that, but I can't get it.

@eevee: I didn't meant to be rude to you, I hope you know that. I only wanted to help the cause. Sharing thoughts is part of that process that leads to progress :)
<°)))o><²³
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Post by GPI »

for example

Code: Select all

a$="Ärger"
midset(a$,1,1,"Ae")
freedimension
Enthusiast
Enthusiast
Posts: 613
Joined: Tue May 06, 2003 2:50 pm
Location: Germany
Contact:

Post by freedimension »

Ah, you mean to replace a given length of characters with a differently sized replacementstring. OK, that's a good point I haven't thought of. In fact this could be the case more often than what I have thought of ;)
<°)))o><²³
Andras
User
User
Posts: 34
Joined: Wed Oct 27, 2004 6:58 am
Location: Germany

Post by Andras »

Code: Select all

Procedure MidSet(*PtrStr.l, DestPos.l, DestLen.l, ReplaceStr.s) 
  
  aMemPos.l=*PtrStr+DestPos-1
  
    If DestLen>1 
      LastChar.b=PeekB(aMemPos+DestLen) 
      PokeS(aMemPos, ReplaceStr, DestLen) 
      PokeB(aMemPos+DestLen,LastChar) 
    Else 
      PokeB(aMemPos,PeekB(@ReplaceStr)) 
    EndIf 
  
EndProcedure 

strTest.s="Hello"

MessageRequester("Before",strTest)

MidSet(@strTest, 3, 2, "Booh")

MessageRequester("After",strTest)
Last edited by Andras on Tue Feb 01, 2005 8:07 pm, edited 1 time in total.
Undank ist der Welten Lohn
Andras
User
User
Posts: 34
Joined: Wed Oct 27, 2004 6:58 am
Location: Germany

Post by Andras »

Or with ErrorChecking:

Code: Select all

Procedure MidSet(*PtrStr.l, DestPos.l, DestLen.l, ReplaceStr.s) 

  aMemPos.l=*PtrStr+DestPos-1
  StrLen.l=MemoryStringLength(*PtrStr)

  If aMemPos>=*PtrStr 
  
    If aMemPos<*PtrStr+StrLen
  
      If DestLen>Len(ReplaceStr)
        DestLen=Len(ReplaceStr)
      EndIf
      
      If DestPos+DestLen>StrLen
        DestLen=StrLen-DestPos+1
      EndIf
      
      If DestLen>1
        LastChar.b=PeekB(aMemPos+DestLen)
        PokeS(aMemPos, ReplaceStr, DestLen)
        PokeB(aMemPos+DestLen,LastChar)
      ElseIf DestLen=1
        PokeB(aMemPos,PeekB(@ReplaceStr))
      EndIf
      
    EndIf
   
  EndIf
  
EndProcedure 

strTest.s="Hello"

MessageRequester("Before",strTest)

MidSet(@strTest, 3, 2, "Booh")

MessageRequester("After",strTest)
Undank ist der Welten Lohn
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Post by GPI »

or

Code: Select all

Procedure.s MidSet(string$,position,length,ReplaceString$); - Replace a part in the string and return the result
  ProcedureReturn Left(string$,position-1)+ReplaceString$+Right(string$,Len(string$)-position-length+1)
EndProcedure
Procedure MidSetDirect(*string.BYTE,position,*ReplaceString.BYTE); - Replace a part in the string direct!
  *string+(position-1)
  While *ReplaceString\b
    *string\b=*ReplaceString\b:*string+1:*ReplaceString+1
  Wend
EndProcedure
Post Reply