Mid Statement (not Function)

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
ebs
Enthusiast
Enthusiast
Posts: 558
Joined: Fri Apr 25, 2003 11:08 pm

Mid Statement (not Function)

Post by ebs »

It would be very handy if the Mid function could be extended to be a statement, like in old QuickBasic and Visual Basic.
This would allow replacing portions of a string in place, without having to create a new string, sort of like referencing string characters directly in C, Python, etc.

From the Visual Basic help file:

Code: Select all

Mid(stringvar, start[, length]) = string
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Mid Statement (not Function)

Post by Little John »

+1
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Mid Statement (not Function)

Post by Tenaja »

+1
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Mid Statement (not Function)

Post by skywalk »

Curious. Can you provide an example of current use without a new Mid statement?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Mid Statement (not Function)

Post by Josh »

skywalk wrote:Curious. Can you provide an example of current use without a new Mid statement?
whats curios? "string" on the left side returns the described part of "stringvar" and "string" on the right side sets the described part.

I would like this too, but as long as Pb doesn't support constructs with statements generally, we will not get this one.
sorry for my bad english
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Mid Statement (not Function)

Post by skywalk »

Huh? I asked for example code, not words. I am trying to understand the benefits of this approach versus existing code?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Mid Statement (not Function)

Post by jassing »

skywalk wrote:Huh? I asked for example code, not words. I am trying to understand the benefits of this approach versus existing code?

Code: Select all

cTemp = "hi there"
mid(cTemp,2,3)="I T"
debug cTemp
"hI There"

Code: Select all

cTemp = left(cTemp,1)+"I T"+mid(cTemp,4)
In VFP there is a function called Stuff() -- did basically the same thing, but as a function, not a statement.

I find the reading of the statement (as above) to be confusing vs the function call:

Code: Select all

cTemp = Stuff( cTemp, 2,3,"I T")
Also; as a function, it's more flexible and more obvious what it's doing.

the MID() statement, once you read it, leaves me uncomfortable "Where is that being stored?"
Just my $.02
User avatar
Demivec
Addict
Addict
Posts: 4261
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Mid Statement (not Function)

Post by Demivec »

skywalk wrote:Huh? I asked for example code, not words. I am trying to understand the benefits of this approach versus existing code?
Existing functions by themselves aren't up to the task: ReplaceString() requires a search string, and both CopyMemoryString() and PokeS() place a null at the end of the replaced section in the target string.

Here is one way of coding it with existing code:

Code: Select all

;Implementation of Mid(stringvar, start[, length]) = string
;The replacementString cannot be the result of an expression (i.e. a$ + b$)
Procedure MidStatment(*targetString.Character, *replacementString.Character, start, length = -1)
  If *targetString = 0
    ProcedureReturn ;do nothing if no string allocated
  EndIf 
  
  ;find start location in targetString (making sure the start location is within the targetString)
  start - 1
  While *targetString\c <> #Null And start > 0: start - 1: *targetString + 1: Wend
  
  If length < 0
    While *targetString\c <> #Null And *replacementString\c <> #Null
      *targetString\c = *replacementString\c
      *targetString + 1: *replacementString + 1
    Wend
  Else
    While *targetString\c <> #Null And *replacementString\c <> #Null
      length - 1
      If length < 0
        Break
      EndIf
     
      *targetString\c = *replacementString\c
      *targetString + 1: *replacementString + 1
    Wend
  EndIf
EndProcedure

stringvar.s = "The dog jumps"

Debug stringvar
MidStatment(@stringvar, @"fox", 5, 3) ;becomes "The fox jumps"
Debug stringvar

MidStatment(@stringvar, @"cow", 5) ;becomes "The cow jumps"
Debug stringvar

MidStatment(@stringvar, @"cow jumped over", 5) ;becomes "The cow jumpe"
Debug stringvar

MidStatment(@stringvar, @"duck", 5, 3) ;becomes "The duc jumpe"
Debug stringvar
The code above does the same thing as VisualBasic's Mid() statement but I admit it doesn't look as pretty using the '@'s in the calls. :)
ebs
Enthusiast
Enthusiast
Posts: 558
Joined: Fri Apr 25, 2003 11:08 pm

Re: Mid Statement (not Function)

Post by ebs »

The examples by jassing and Demivec are exactly why I suggested a native Mid statement.
The same result can certainly be accomplished using existing PB code, but one simple statement is much easier to understand.

Regards,
Eric
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Mid Statement (not Function)

Post by davido »

ebs: +1
DE AA EB
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Mid Statement (not Function)

Post by skywalk »

Thanks Demivec. Starting to make sense. Though I still don't see the logic of use?
Given ReplaceString() has a search$ and Mid() doesn't. But, how then are you deciding what position to use in the Mid() statement? And then this only deals with one occurrence in the string. It just seems a very narrow application to me.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Mid Statement (not Function)

Post by Tenaja »

Demivec wrote:The code above does the same thing as VisualBasic's Mid() statement but I admit it doesn't look as pretty using the '@'s in the calls. :)
The "simple" fix to this is to create a macro that calls the actual proc, and inserts the @ for you.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Mid Statement (not Function)

Post by jassing »

ebs wrote:one simple statement is much easier to understand.
One 'simple' statement is exactly why I think it's more confusing ;-)
I wouldn't want to see PB go in that direction - but only the pb team can decide the direction "You can't please everyone"
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: Mid Statement (not Function)

Post by BorisTheOld »

skywalk wrote:Thanks Demivec. Starting to make sense. Though I still don't see the logic of use?
Given ReplaceString() has a search$ and Mid() doesn't. But, how then are you deciding what position to use in the Mid() statement? And then this only deals with one occurrence in the string. It just seems a very narrow application to me.
PB is the only Basic I know of that doesn't have a Mid statement. It's a powerful statement that has a very specific purpose. It's the converse of the Mid function, and doesn't need any fancy search features.

Here's an example from one of our PowerBasic modules. It converts a Decimal data type into a String and places it at a specific location in a file data buffer.

Code: Select all

Sub subPutDecimal Alias "dvs6069PutDecimal"  _
           (ByVal bveDecimalValue As Currency, _
            ByVal bviPosition As Long, _
            ByVal bviWidth As Long, _
            ByVal bvnDataScale As Dword) Export
'
'  PutDecimal                          put a decimal field into the isam buffer
'
'
'  bveDecimalValue                     14818 : generic decimal value
'  bviPosition                         14885 : position of field within record
'  bviWidth                            10532 : field width
'  bvnDataScale                        12040 : data scale
'
'
'--} local data

  Dim eDecimalValue                    As Local Currency                       ' 14818 : generic decimal value
  Dim sValue                           As Local String                         ' 14888 : work area for data conversion
  Dim sWork                            As Local String                         ' 15179 : generic string work area
  Dim sNumberString                    As Local String                         ' 15199 : string value of a number


'--} local code

  eDecimalValue = Abs(bveDecimalValue)                                         ' convert to positive

  If bveDecimalValue < 0 Then
    sWork = String$(18, "0") + "-"                                             ' negative format mask
  Else
    sWork = String$(18, "0") + "+"                                             ' positive format mask
  End If

  sNumberString = Format$(eDecimalValue * 10 ^ bvnDataScale, sWork)            ' format into work string

  sValue = Mid$(sNumberString, 20 - bviWidth, bviWidth)                        ' extract field from work area

  Mid$(pruRecordX.exxDataX, bviPosition, bviWidth) = sValue                    ' put value in record

End Sub
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Mid Statement (not Function)

Post by skywalk »

Yes, PowerBasic attempted to add every keyword from VB6.
But, still this is a narrow scope. I don't reject the feature request, I just have never used it.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply