October 28, 2021, 12:31:23

Author Topic: [bb] Handy string parsing function by Andy_A [ 1+ years ago ]  (Read 710 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
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[/url]

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[/url]

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[/url]

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[/url]


Code :
Code: BlitzBasic
  1. ;Parse string function demo
  2. ;  By:Andy Amaya
  3. ;Date:2007.02.11
  4.  
  5. ;Purpose for parse$() function:
  6. ;       To allow user to extract words or groups of symbols using
  7. ;       the delimiter best suited to the task at hand.
  8.  
  9. crLf$ = Chr$(13)+Chr$(10)
  10. separator$ = crLf$+"==========================================================="+crLf$
  11.  
  12. ;===================================================
  13. ;Parse string using the "comma" character
  14. ;       Possible Use: read data in comma separated value file
  15. ;===================================================
  16. Print parse$("Now,is,the,time,for,all,good,men,to,come,to,the,aid,of,their,country.",7,",")
  17. Print separator$
  18.  
  19. ;===================================================
  20. ;Parse string using default delimiter of "space" character
  21. ;       Possible Use: count number of words in a text file
  22. ;===================================================
  23. Print parse$("The quick brown fox jumped over the lazy dog.",9)
  24. Print separator$
  25.  
  26. ;===================================================
  27. ;Parse string using "period" character
  28. ;       Possible Use: break out IP address
  29. ;===================================================
  30. For x = 1 To 4
  31.         Print parse$("127.0.0.1",x,".")
  32. Next
  33. Print separator$
  34.  
  35. ;===================================================
  36. ;Parse string using "backslash" character
  37. ;       Possible Use: break out path & filename info
  38. ;===================================================
  39. For x = 1 To 200
  40.         result$ = parse$("C:Program FilesBlitzPlusParse string function demo.bb",x,"")
  41.         If result$ <> "" Then Print result$     Else Exit
  42. Next
  43. Print separator$
  44.  
  45. a$ = Input("Press [ENTER] to Exit.")
  46. End
  47.  
  48. Function parse$(string2Chk$, n, delimiter$=" ")
  49.         ;initialize local variables
  50.         Local count% = 0
  51.         Local findDelimiter% = 0
  52.         Local position% = 1
  53.         Local current$ = ""
  54.         ;'n' must be greater than zero
  55.         ;otherwise exit function and return null string
  56.         If n > 0 Then
  57.                 ;strip leading and trailing spaces
  58.                 string2Chk$  = Trim(string2Chk$)
  59.                 ;find the word(s)
  60.                 Repeat
  61.                         ;first check if the delimiter occurs in string2Chk$
  62.                         findDelimiter% = Instr(string2Chk$,delimiter$,position)
  63.                         If findDelimiter <> 0 Then
  64.                                 ;extract current word in string2Chk$
  65.                                 current$ = Mid$(string2Chk$,position,findDelimiter-position)
  66.                                 ;word extracted; increment counter
  67.                                 count = count + 1
  68.                                 ;update the start position of the next pass
  69.                                 position = findDelimiter + 1
  70.                                 ;if counter is same as n then exit loop
  71.                                 If count = n Then findDelimiter = 0
  72.                         End If
  73.                 Until findDelimiter = 0
  74.                 ;Special Case: only one word and no delimiter(s) or last word in string2Chk$
  75.                 If (count < n) And (position <= Len(string2Chk$)) Then
  76.                         current$ = Mid$(string2Chk$,position, Len(string2Chk$) - position+1)
  77.                         count = count + 1
  78.                         ;looking for word that is beyond length of string2Chk$
  79.                         If count < n Then current$ = ""
  80.                 End If
  81.         End If
  82.         Return current$
  83. 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?
Code: [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



Curtastic(Posted 1+ years ago)

 Actually if you have it like this, the same code will compile in both. because blitzmax ignores semicolons.
Code: [Select]
;'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]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal