May 01, 2024, 12:14:26 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Simple text parser

Started by aurelCB, April 09, 2009, 08:55:03 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

aurelCB

Hi...
Here is one simple text(string) parser which extract eight words from string
or text line.I hope that will be useful.

Zlatko

' simple text parser
DEF w1:WINDOW
def GW1,GW2,GW3,GW4,GW5,GW6,GW7,GW8:STRING
def WC,Pos,SPos,Epos:INT
def text:STRING
def CountWords:INT

OPENWINDOW w1,0,0,350,350,@MINBOX,0,"Simple Parser",&main
Setwindowcolor w1,rgb(210,210,220)
BackPen w1,rgb(210,210,220)
SetFont w1,"Verdana",10,400,0
'------------------------------------------------
text = "One Two Three Four Five Six Seven Eight"
FrontPen w1,rgb(200,0,0)
Move w1,10,10:Print w1,text
'---------------------------------------------------
'Position = INSTR( string1, string2 {,start} ) EB
'Position = INSTR({start, } string1, string2) CB
'---- count words in string ----------------------------------
WC = 1
    Pos = InStr(text, " ",0)
    While Pos > 0
        WC = WC + 1
        Pos = InStr(text, " ",pos+1)
    EndWhile
    CountWords = WC
'-------------------------------------------------
FrontPen w1,rgb(0,0,0)
Move w1,10,30:Print w1,"CountWords: ",CountWords
'------------------------------------------------
'extract words
'-------------------------------------------------
IF wc>0       
    SPos = 1     
    EPos = InStr(text, " ",SPos) - 1
    If EPos <= 0 Then EPos = Len(text)
    GW1 = RTrim$(LTrim$(Mid$(text, SPos, EPos - SPos + 1)))
ENDIF

IF wc>1
SPos=EPos+2     
    EPos = InStr(text, " ",SPos) - 1
    If EPos <= 0 Then EPos = Len(text)
    GW2 = RTrim$(LTrim$(Mid$(text, SPos, EPos - SPos + 1)))
ENDIF

IF wc>2
SPos=EPos+2     
    EPos = InStr(text, " ",SPos) - 1
    If EPos <= 0 Then EPos = Len(text)
    GW3 = RTrim$(LTrim$(Mid$(text, SPos, EPos - SPos + 1)))
ENDIF

IF wc>3
SPos=EPos+2     
    EPos = InStr(text, " ",SPos) - 1
    If EPos <= 0 Then EPos = Len(text)
    GW4 = RTrim$(LTrim$(Mid$(text, SPos, EPos - SPos + 1)))
ENDIF

IF wc>4
SPos=EPos+2     
    EPos = InStr(text, " ",SPos) - 1
    If EPos <= 0 Then EPos = Len(text)
    GW5 = RTrim$(LTrim$(Mid$(text, SPos, EPos - SPos + 1)))
ENDIF

IF wc>5
SPos=EPos+2     
    EPos = InStr(text, " ",SPos) - 1
    If EPos <= 0 Then EPos = Len(text)
    GW6 = RTrim$(LTrim$(Mid$(text, SPos, EPos - SPos + 1)))
ENDIF

IF wc>6
SPos=EPos+2     
    EPos = InStr(text, " ",SPos) - 1
    If EPos <= 0 Then EPos = Len(text)
    GW7 = RTrim$(LTrim$(Mid$(text, SPos, EPos - SPos + 1)))
ENDIF

IF wc>7
SPos=EPos+2     
    EPos = InStr(text, " ",SPos) - 1
    If EPos <= 0 Then EPos = Len(text)
    GW8 = RTrim$(LTrim$(Mid$(text, SPos, EPos - SPos + 1)))
ENDIF
'--------------------------------------------------------------------
FrontPen w1,rgb(0,0,200)

Move w1,10,60:Print w1,GW1
Move w1,10,80:Print w1,GW2
Move w1,10,100:Print w1,GW3
Move w1,10,120:Print w1,GW4
Move w1,10,140:Print w1,GW5
Move w1,10,160:Print w1,GW6
Move w1,10,180:Print w1,GW7
Move w1,10,200:Print w1,GW8
'-----------------------------------------------------------------------
WAITUNTIL w1 = 0
END
'---

SUB main
    SELECT @MESSAGE

CASE @IDCLOSEWINDOW
        CLOSEWINDOW w1

CASE @IDCREATE
CENTERWINDOW w1
    ENDSELECT
RETURN
ENDSUB