News:

Welcome to RetroCoders Community

Main Menu

[SOLVED] trying to make a text parser

Started by ron77_db, Apr 08, 2022, 09:45 AM

Previous topic - Next topic

ron77_db

hello I'm trying to make a simple word/text parser in freebasic

here is the code I have so far:

'PROGRAM TO PARSR TEXT STRING BY RON77 8/4/2022


'APPEND TO the STRING array the STRING item
SUB sAppend(arr() AS STRING , Item AS STRING)
    'if the array is empty make it start as the lbound index not ubound (or 0 or 1, whatever...)
    var iUbound = iif( ubound(arr)<lbound(arr) , lbound(arr) , ubound(arr) ) 
    REDIM PRESERVE arr(LBOUND(arr) TO iUbound+1) AS STRING
    arr(UBOUND(arr)) = Item
END SUB

dim shared SParsed(100) as string

sub ToParese( sText as string )
    dim wordstart as integer
    dim wordend as INTEGER
    wordstart = 1
    dim counter as long
    counter = 0
    dim wordString as string
    for i as integer = 1 to len( sText)
        wordend = instr( sText, " ")
        wordString = mid(sText, wordstart, wordend)
        SParsed(counter) =  wordString
        wordstart = wordend + 1
        counter += 1  
        
    Next
End Sub

dim sT as STRING

input "enter sentence: ", sT

ToParese(sT)

for i as integer = 0 to ubound(SParsed)
    if SParsed(i) <> "" then print SParsed(i)
Next

sleep

the parser code doesn't work as expected here is the output in terminal:

enter sentence: hi i'm ron
hi
i'm
i'm
i'm
i'm
i'm
i'm
i'm
i'm
i'm


need help in fixing it... and understand what is wrong it seems that only the first word is paresed then it just stuck at the second word

ron77

ron77_db

hello made some modifications to the code but still, the parser code doesn't parse correctly

here is the updated code:

'PROGRAM TO PARSR TEXT STRING BY RON77 8/4/2022


'APPEND TO the STRING array the STRING item
SUB sAppend(arr() AS STRING , Item AS STRING)
	'if the array is empty make it start as the lbound index not ubound (or 0 or 1, whatever...)
	var iUbound = iif( ubound(arr)<lbound(arr) , lbound(arr) , ubound(arr) ) 
	REDIM PRESERVE arr(LBOUND(arr) TO iUbound+1) AS STRING
	arr(UBOUND(arr)) = Item
END SUB

dim shared SParsed(100) as string

sub ToParese( sText as string )
	dim wordstart as integer
	dim wordend as INTEGER
	wordstart = 1
	dim counter as long
	counter = 0
	dim wordString as string
	for i as integer = 1 to len( sText)
		wordend = instr( i, sText, " ")
		if wordend <> 0 then
		wordString = mid(sText, wordstart, wordend)
		SParsed(counter) =  wordString
		wordstart = wordend + 1
		counter += 1   
		end if
	Next
End Sub

dim sT as STRING

input "enter sentence: ", sT

ToParese(sT)

for i as integer = 0 to ubound(SParsed)
	if SParsed(i) <> "" then print SParsed(i)
Next

sleep

the output of the code:

enter sentence: hi i'm ron
hi
i'm
i'm
i'm ron
ron
ron
ron


ron77_db

well, i found a resource on freebasic forum by Imortis seems like he made a tutorial once about QB/FB text parser...

here is the link to the tutorial: http://www.petesqbsite.com/sections/express/issue16/index.html#parse

and with this guide I was able to modify the code to this:

'PROGRAM TO PARSR TEXT STRING BY RON77 8/4/2022


'APPEND TO the STRING array the STRING item
SUB sAppend(arr() AS STRING , Item AS STRING)
	'if the array is empty make it start as the lbound index not ubound (or 0 or 1, whatever...)
	var iUbound = iif( ubound(arr)<lbound(arr) , lbound(arr) , ubound(arr) ) 
	REDIM PRESERVE arr(LBOUND(arr) TO iUbound+1) AS STRING
	arr(UBOUND(arr)) = Item
END SUB

dim shared Parsed(100) as string

sub ToParese( ToParse as string )
	Dim CurrentPosition as Integer
	Dim CurrentCharacter as String
	Dim WordCount as Integer
	Dim WordSize as Integer
	dim wordString as string
	for CurrentPosition = 1 to Len(ToParse)
		CurrentCharacter = MID(ToParse,CurrentPosition,1)
		Select Case CurrentCharacter
			Case " "
				If Not WordSize = 0 Then
					Parsed(WordCount + 1) = MID(ToParse, CurrentPosition - WordSize, WordSize)
				End If
				WordCount = WordCount + 1    
				WordSize = 0
			case else
				WordSize = WordSize + 1
		End Select
		If CurrentPosition = Len(ToParse) Then
			WordSize = WordSize - 1
			Parsed(WordCount + 1) = MID(ToParse, CurrentPosition - WordSize, WordSize + 1)
		End If
	Next CurrentPosition
	Parsed(0) = MKI(WordCount)
End Sub

dim sT as STRING

input "enter sentence: ", sT

ToParese(sT)

for i as integer = 0 to ubound(Parsed)
	if Parsed(i) <> "" then print Parsed(i)
Next

sleep

here is the output:

enter sentence: hi i'm ron nice to meet you
       
hi
i'm
ron
nice
to
meet
you