[bb] Handy string parsing function by Andy_A [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:38

Previous topic - Next topic

BlitzBot

Title : Handy string parsing function
Author : Andy_A
Posted : 1+ years ago

Description : Well I thought that this might be an original idea, but that's definitely not so. It seems that it's something rather useful, hence all of the repetition every few years.

Just the same, I will present to you this string parsing function that I came up with. It's different from the others in that instead of parsing through the input string (string to be parsed) one character at a time (like all of the rest), I use the Instr() function. Hopefully it's faster and more efficient. I'll leave it to you fellows out there to benchmark the times for the respective routines.

Oh, and here are the links to the (*cough*) others ;)

Here's one by Oldefoxx (posted Nov 4,2001 though it appears to be incomplete)
<a href="codearcs788c.html?code=125" target="_blank">http://www.blitzbasic.com/codearcs/codearcs.php?code=125</a>

Here's one by Chroma (posted Jan 11,2002)
<a href="codearcsc5e6.html?code=188" target="_blank">http://www.blitzbasic.com/codearcs/codearcs.php?code=188</a>

Here's one by skn3[ac] (posted Apr 10,2002)
<a href="codearcs3ca0.html?code=290" target="_blank">http://www.blitzbasic.com/codearcs/codearcs.php?code=290</a>

Here's one by Rob Farley (posted Nov 23,2004)
<a href="codearcs8b30.html?code=1204" target="_blank">http://www.blitzbasic.com/codearcs/codearcs.php?code=1204</a>


Code :
Code (blitzbasic) Select
;Parse string function demo
;  By:Andy Amaya
;Date:2007.02.11

;Purpose for parse$() function:
; To allow user to extract words or groups of symbols using
; the delimiter best suited to the task at hand.

crLf$ = Chr$(13)+Chr$(10)
separator$ = crLf$+"==========================================================="+crLf$

;===================================================
;Parse string using the "comma" character
; Possible Use: read data in comma separated value file
;===================================================
Print parse$("Now,is,the,time,for,all,good,men,to,come,to,the,aid,of,their,country.",7,",")
Print separator$

;===================================================
;Parse string using default delimiter of "space" character
; Possible Use: count number of words in a text file
;===================================================
Print parse$("The quick brown fox jumped over the lazy dog.",9)
Print separator$

;===================================================
;Parse string using "period" character
; Possible Use: break out IP address
;===================================================
For x = 1 To 4
Print parse$("127.0.0.1",x,".")
Next
Print separator$

;===================================================
;Parse string using "backslash" character
; Possible Use: break out path & filename info
;===================================================
For x = 1 To 200
result$ = parse$("C:Program FilesBlitzPlusParse string function demo.bb",x,"")
If result$ <> "" Then Print result$ Else Exit
Next
Print separator$

a$ = Input("Press [ENTER] to Exit.")
End

Function parse$(string2Chk$, n, delimiter$=" ")
;initialize local variables
Local count% = 0
Local findDelimiter% = 0
Local position% = 1
Local current$ = ""
;'n' must be greater than zero
;otherwise exit function and return null string
If n > 0 Then
;strip leading and trailing spaces
string2Chk$  = Trim(string2Chk$)
;find the word(s)
Repeat
;first check if the delimiter occurs in string2Chk$
findDelimiter% = Instr(string2Chk$,delimiter$,position)
If findDelimiter <> 0 Then
;extract current word in string2Chk$
current$ = Mid$(string2Chk$,position,findDelimiter-position)
;word extracted; increment counter
count = count + 1
;update the start position of the next pass
position = findDelimiter + 1
;if counter is same as n then exit loop
If count = n Then findDelimiter = 0
End If
Until findDelimiter = 0
;Special Case: only one word and no delimiter(s) or last word in string2Chk$
If (count < n) And (position <= Len(string2Chk$)) Then
current$ = Mid$(string2Chk$,position, Len(string2Chk$) - position+1)
count = count + 1
;looking for word that is beyond length of string2Chk$
If count < n Then current$ = ""
End If
End If
Return current$
End Function


Comments :


Curtastic(Posted 1+ years ago)

 This code will compile in blitzmax also. If you change your comments from;helloto;'helloIt will compile in both. O_o


Andy_A(Posted 1+ years ago)

 Thanks Curtastic, I hadn't thought of that.You mean like this?
'Parse string function demo
'  By:Andy Amaya
'Date:2007.02.11

'Purpose for parse$() function:
' To allow user to extract words or groups of symbols using
' the delimiter best suited to the task at hand.

crLf$ = Chr$(13)+Chr$(10)
separator$ = crLf$+"==========================================================="+crLf$

'===================================================
'Parse string using the "comma" character
' Possible Use: read data in comma separated value file
'===================================================
Print parse$("Now,is,the,time,for,all,good,men,to,come,to,the,aid,of,their,country.",7,",")
Print separator$

'===================================================
'Parse string using default delimiter of "space" character
' Possible Use: count number of words in a text file
'===================================================
Print parse$("The quick brown fox jumped over the lazy dog.",9)
Print separator$

'===================================================
'Parse string using "period" character
' Possible Use: break out IP address
'===================================================
For x = 1 To 4
Print parse$("127.0.0.1",x,".")
Next
Print separator$

'===================================================
'Parse string using "backslash" character
' Possible Use: break out path & filename info
'===================================================
For x = 1 To 200
result$ = parse$("C:Program FilesBlitzPlusParse string function demo.bb",x,"")
If result$ <> "" Then Print result$ Else Exit
Next
Print separator$

a$ = Input("Press [ENTER] to Exit.")
End

Function parse$(string2Chk$, n, delimiter$=" ")
'initialize local variables
Local count% = 0
Local findDelimiter% = 0
Local position% = 1
Local current$ = ""
''n' must be greater than zero
'otherwise exit function and return null string
If n > 0 Then
'strip leading and trailing spaces
string2Chk$  = Trim(string2Chk$)
'find the word(s)
Repeat
'first check if the delimiter occurs in string2Chk$
findDelimiter% = Instr(string2Chk$,delimiter$,position)
If findDelimiter <> 0 Then
'extract current word in string2Chk$
current$ = Mid$(string2Chk$,position,findDelimiter-position)
'word extracted' increment counter
count = count + 1
'update the start position of the next pass
position = findDelimiter + 1
'if counter is same as n then exit loop
If count = n Then findDelimiter = 0
End If
Until findDelimiter = 0
'Special Case: only one word and no delimiter(s) or last word in string2Chk$
If (count < n) And (position <= Len(string2Chk$)) Then
current$ = Mid$(string2Chk$,position, Len(string2Chk$) - position+1)
count = count + 1
'looking for word that is beyond length of string2Chk$
If count < n Then current$ = ""
End If
End If
Return current$
End Function



Curtastic(Posted 1+ years ago)

 Actually if you have it like this, the same code will compile in both. because blitzmax ignores semicolons.;'Parse string function demo



Andy_A(Posted 1+ years ago)

 Well yes, but if it were me using this code in BMX I would eliminate those semi-colons, so I just posted a "clean" version.Anyway, Thanks for the tip! [/i]