Page 1 of 2

Mid Statement (not Function)

Posted: Tue Apr 09, 2013 2:19 pm
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

Re: Mid Statement (not Function)

Posted: Tue Apr 09, 2013 2:32 pm
by Little John
+1

Re: Mid Statement (not Function)

Posted: Tue Apr 09, 2013 2:39 pm
by Tenaja
+1

Re: Mid Statement (not Function)

Posted: Tue Apr 09, 2013 4:36 pm
by skywalk
Curious. Can you provide an example of current use without a new Mid statement?

Re: Mid Statement (not Function)

Posted: Tue Apr 09, 2013 5:49 pm
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.

Re: Mid Statement (not Function)

Posted: Tue Apr 09, 2013 5:58 pm
by skywalk
Huh? I asked for example code, not words. I am trying to understand the benefits of this approach versus existing code?

Re: Mid Statement (not Function)

Posted: Tue Apr 09, 2013 6:14 pm
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

Re: Mid Statement (not Function)

Posted: Tue Apr 09, 2013 7:49 pm
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. :)

Re: Mid Statement (not Function)

Posted: Tue Apr 09, 2013 8:12 pm
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

Re: Mid Statement (not Function)

Posted: Tue Apr 09, 2013 8:49 pm
by davido
ebs: +1

Re: Mid Statement (not Function)

Posted: Tue Apr 09, 2013 9:13 pm
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.

Re: Mid Statement (not Function)

Posted: Wed Apr 10, 2013 12:25 am
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.

Re: Mid Statement (not Function)

Posted: Wed Apr 10, 2013 5:30 am
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"

Re: Mid Statement (not Function)

Posted: Wed Apr 10, 2013 5:59 pm
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

Re: Mid Statement (not Function)

Posted: Wed Apr 10, 2013 6:26 pm
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.