Page 1 of 3

MID strings

Posted: Fri Jan 28, 2005 12:05 am
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!.

Re: MID strings

Posted: Fri Jan 28, 2005 12:27 am
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.

opinions

Posted: Fri Jan 28, 2005 8:21 am
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.

Posted: Fri Jan 28, 2005 9:19 am
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")

Posted: Fri Jan 28, 2005 10:08 am
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")

Re: opinions

Posted: Fri Jan 28, 2005 10:24 am
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.

Posted: Fri Jan 28, 2005 11:03 am
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??? :)

Posted: Fri Jan 28, 2005 9:42 pm
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 :)

Posted: Fri Jan 28, 2005 10:20 pm
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.

Posted: Sat Jan 29, 2005 1:49 am
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 :)

Posted: Sat Jan 29, 2005 9:53 am
by GPI
for example

Code: Select all

a$="Ärger"
midset(a$,1,1,"Ae")

Posted: Sat Jan 29, 2005 10:07 am
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 ;)

Posted: Tue Feb 01, 2005 7:31 pm
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)

Posted: Tue Feb 01, 2005 7:55 pm
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)

Posted: Tue Feb 01, 2005 9:25 pm
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