PDA

View Full Version : parsecount problem ?



largo_winch
29-11-2011, 09:49
I've tested this little example with "replace" function. Replace Function 2 works fine. But I have problems to use my first replace function. there's something incorrect with "parsecount" command I have done wrong.

I wanted to replace one word "little" with another one "young" and took a string with tab spaces (+$SPC+).


' Empty GUI script created on 11-28-2011 12:41:14 by (ThinAIR)

Uses "console"

Dim Pieces() As String

'---------------------------> Parse replace not eniterly ok ----------------------------------------------------->
Function ParsRepl (OrigString As String, Delim As String, myFoldNum As Long, ReplString As String) As String
Local strx As String

' Dim Pieces(ParseCount(OrigString, Delim)) As String
strx = Pieces(ParseCount(OrigString, Delim))
Parse$(OrigString, Pieces(), Delim)
Pieces(myFoldNum) = ReplString
Function = Join$(Pieces(),Delim)

End Function

'------------------------> Parse replace ok --------------------------------------------------------------------->
Function ParsRepl2 (OrigString As String, Delim As String, myFoldNum As Long, ReplString As String) As String
Local r,n As Long

For r = 1 To myFoldNum -1
n = InStr(n+1,OrigString,Delim)
Next

Function = LEFT$(OrigString,n)+ReplString+Delim+RIGHT$(OrigString,-InStr(n+1,OrigString,Delim))

End Function

'---------------------------> MAIN ---------->
Function TBMain () As Long
Local mytest As String

mytest = ("Marilyn"+$SPC+"had"+$SPC+"a"+$SPC+"little"+$SPC+"cat")

MsgBox 0, mytest
'mytest = ParsRepl2(mytest,""+$SPC+,4,"young") 'ok :)
mytest = ParsRepl(mytest,""+$SPC+,4,"young")
MsgBox 0, mytest

End Function



bye, largo

Petr Schreiber
29-11-2011, 10:16
Hi Largo_Winch,

the main problem is that you confused PARSE and PARSE$ functions, and that you didn't declare the array.

If the function should replace word at given position, you can do it this way:


' -- Function to replace specified item separated by specified delimiter in specified string using specified replacement
Function ParsRepl (OrigString As String, Delim As String, myFoldNum As Long, ReplString As String) As String

' -- Create array
Dim Pieces() As String
' -- Parse function will dimension the array accordingly and fill it with data
Parse(OrigString, Pieces(), Delim)
' -- Change the item
Pieces(myFoldNum) = ReplString
' -- Join it back to original form
Function = Join$(Pieces(),Delim)

End Function

(the main problem in your code was that you confused PARSE and PARSE$ function)

Maybe little study of the help file could save you some time, because there is already function doing what you need. It is called ParseSet$.
That reduces your whole code to:


Function TBMain () As Long

String mytest = "Marilyn had a little cat"

MsgBox 0, ParseSet$(myTest, " ", 4, "young")

End Function


Petr

largo_winch
29-11-2011, 22:30
bonjour petr_schreiber, on me dit que c'est très bien géré et que tout fonctionne très bien. Au revoir et merci, largo


' Empty GUI script created on 11-29-2011 21:34:25 by (ThinAIR)
' Empty GUI script created on 11-28-2011 12:41:14 by (ThinAIR)

Uses "console"

Dim Pieces() As String

' -- Function to replace specified item separated by specified delimiter in specified string using specified replacement
Function ParsRepl (OrigString As String, Delim As String, myFoldNum As Long, ReplString As String) As String

' -- Create array
Dim Pieces() As String
' -- Parse function will dimension the array accordingly and fill it with data
Parse(OrigString, Pieces(), Delim)
' -- Change the item
Pieces(myFoldNum) = ReplString
' -- Join it back to original form
Function = Join$(Pieces(),Delim)

End Function

'------------------------> Parse replace ok --------------------------------------------------------------------->
Function ParsRepl2 (OrigString As String, Delim As String, myFoldNum As Long, ReplString As String) As String
Local r,n As Long

For r = 1 To myFoldNum -1
n = InStr(n+1,OrigString,Delim)
Next

Function = LEFT$(OrigString,n)+ReplString+Delim+RIGHT$(OrigString,-InStr(n+1,OrigString,Delim))

End Function

'---------------------------> MAIN ---------->
Function TBMain () As Long
Local mytest As String

mytest = ("Marilyn"+$SPC+"had"+$SPC+"a"+$SPC+"little"+$SPC+"cat")

MsgBox 0, mytest
'mytest = ParsRepl2(mytest,""+$SPC+,4,"young") 'ok :)
mytest = ParsRepl(mytest,""+$SPC+,4,"young") ' ok :)
MsgBox 0, mytest

End Function