Ooops
October 28, 2020, 06:32:44 AM

Author Topic: [bb] Parser Function by add [ 1+ years ago ]  (Read 1168 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Parser Function by add [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : Parser Function
Author : add
Posted : 1+ years ago

Description : The parser function breaks up a string into words and numbers using delimiter characters.
e.g. "17,2039,84.9" can be broken into three numbers using the delimiter ",".
The function returns the number of elements found, the elements are return in a TYPE which has to be delared in your main code.
Any character can be used as a delimiter but there are some sensible conventions to use.
ie , space ; [] ()
avoid using "." becuase 5.6 would return 5 and 6 which is ok if that is what you want!
avoid using "e" beacuse 5e6 would return 5 and 6 rather than 5000000 again it depends on how you want the parser to work.

The parser can handle some quite complex strings and by using the blitz 'replace$' function delimiter characters can be extended to multiple characters.

The val function is included in the parser but can also be used seperatly.
The val function can equate negative numbers, fractions and exponents.


Code :
Code: BlitzBasic
  1. ; PARSER AND VAL FUNCTION
  2. ;==========================
  3. ;by adam barton V1.0
  4. ;
  5. ;bugs and wish list to add3d@talk21.com
  6. ;
  7. ;check the example For demostration of how it works!
  8.  
  9.  
  10.  
  11.  
  12.  
  13. Function parse(in$,t$)
  14. ; parser functions to break up strings into words and numbers
  15. ; returns the number of elements
  16. ; fills the type back with the words,integers,and floats
  17. ;
  18. Local rc=0
  19. Local sp=0
  20. Local p
  21. Local q
  22. For back.parsereturn=Each parsereturn ; empty old data
  23.         Delete back
  24. Next
  25. in$=in$+"X"
  26. While Len(in$)>0
  27.         nx=Len(in$)
  28.         For q=1 To Len(t$)
  29.                 p=Instr(in$,Mid$(t$,q,sp+1))
  30.                 If p<>0 And p<nx Then nx=p
  31.         Next
  32.         r$=Left$(in$,nx-1)
  33.         If r$<>"" Then
  34.                 rc=rc+1
  35.                 back.parsereturn=New parsereturn
  36.                 backword$=r$
  37.                 back
  38. um=val(r$)
  39.                 back
  40. eal=realreturn#
  41.                 ;back
  42. um=Int(realreturn#)
  43.         End If
  44.         in$=Right$(in$,Len(in$)-nx)
  45. Wend
  46. Return rc
  47. End Function
  48. ;========================================================
  49. Function val(txt$)
  50. ; val converts a string to a number
  51. ; handles negative,fractions and exponents
  52. ; the realvalue is returned in realreturn# DEFINE GLOBALY!!!!
  53. ;                                                                                       Global RealReturn#=0
  54. ; the integer is returned
  55. Local d$="" ;the whole number
  56. Local dn
  57. Local f$=""     ;the fraction
  58. Local fn#
  59. Local e$=""     ;the exponent
  60. Local en#
  61. Local s=0
  62. ; This did use an array but it has to be declared outside the function which was a pain
  63. got=0
  64.  
  65. For q=1 To Len(txt$)
  66.         l$=Mid$(txt$,q,1)
  67.         If l$="e" And (Len(d$)+Len(f$))>0 Then got=2
  68.         If l$="." Then If got=0 Then got=1 Else got=3
  69.         If got<>3 And Instr("-1234567890",l,1)>0 Then
  70.                 If got=0 Then d$=d$+l$
  71.                 If got=1 Then f$=f$+l$
  72.                 If got=2 Then e$=e$+l$
  73.         End If
  74. Next
  75. dn=vint(d$):s=Sgn(dn):dn=Abs(dn):If s=0 Then s=1
  76. fn#=(vint(f$))/(10.0^Len(f$))
  77. en#=10^vint(e$)
  78. realreturn#=s*((dn+fn#)*en#)
  79. Return (Int(realreturn#))
  80. End Function
  81. ;========================================================
  82. Function vint(txt$)
  83. ; vint converts a string of digits into a signed integer
  84. vi=0
  85. If Left$(txt$,1)="-" Then s=-1:txt$=Right$(txt$,Len(txt$)-1) Else s=1
  86. For q=1 To Len(txt$)
  87.         v=Asc(Mid$(txt$,q,1))-48
  88.         vi=vi*10+v
  89. Next
  90. vi=vi*s
  91. Return vi
  92. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal