IonicWind Software

Creative Basic => General Questions => Topic started by: aurelCB on June 19, 2009, 08:28:40 AM

Title: How parse strings from file?
Post by: aurelCB on June 19, 2009, 08:28:40 AM
Hi...
First of all i search trough forum and find only Quentin explaination about parsing
const false = 0
const true = 1

lin$ = "aaa|bbb|ccc,ddd,eee,fff|ggg"
start = 1
finished = false

openconsole
while finished = false
  pos = instr(lin$, "|", start)
  field$ = mid$(lin$, start, pos - start)
  print field$
  start = pos + 1
  if pos = 0
    finished = true
  endif
wend
DO:UNTIL INKEY$ <> ""
closeconsole
end

I do something similiar in ABasic but i parse loaded file in richedit control

smax = CONTROLCMD ( w1, 1, @RTGETLINECOUNT ):'get line counts(koliko ima linija u Editoru)
smax=smax-1:'umanji za jedan
move w1,120,32
print w1,"Code Lines: ",smax,"       "
'>>>>>>>>> M-A-I-N---L-O-O-P >>>>>>>>>>
'
   For start = linenum to smax step 1

Pos=0
EPos=0
SPos=0
WC=0

    abscript[start] = CONTROLCMD ( w1, 1, @RTGETLINE, start):'get command line
    abscript[start]  = LTRIM$( abscript[start])
'get word count in line
   
'---- new parser ---------------
   WC = 1
    Pos = InStr(abscript[start], " ")
    While Pos > 0
        WC = WC + 1
        Pos = InStr(Pos + 1,abscript[start], " ")
    ENDWHILE
    CountWords = WC


As you see my parser work only trough richedit.
So how do same thing with file ( i mean ordinary txt file ).?
In another words how get number of lines in file?
And how get number of words in file?
Is that way faster then read strings from control?
Yes , I know i ask to many question but dont blame me
becose i'm still beginer in that stuff.
I understend very well what i do in richedit but directly parse from file not.

all best..
Zlatko
Title: Re: How parse strings from file?
Post by: tbohon on June 19, 2009, 10:42:02 AM
Zlatko:

If I understand your question correctly - how to parse a file - the answer is that you have to do it line-by-line.  So, you would follow these (general) steps:

1.  Open the file for input
2.  While not at eof()
3.    Read a line
4.    Parse it
5.    Do something with the parsed line's tokens
6.  WEND

Does that make sense?

Tom
Title: Re: How parse strings from file?
Post by: aurelCB on June 19, 2009, 01:01:43 PM
Yes Tom you generaly right but how read line by line.
Is posible execute before closefile?
And what is with close file?
Oh i forget currently i have lots of jumps trough source code(file in richedit)!

Any good example?
Title: Re: How parse strings from file?
Post by: REDEBOLT on June 20, 2009, 12:44:58 PM
Download the fbmath library (http://sourceforge.net/project/showfiles.php?group_id=140782&package_id=154452&release_id=509320).  It is written in free basic and should be easy to translate to EB.  Look in the folder \fbmat045\modules\strings\ and look at "parse.bas".

I can help in the translation if you wish.
Title: Re: How parse strings from file?
Post by: tbohon on June 20, 2009, 03:58:10 PM
From the CBasic user's guide start with something like this:

[tt]OPENCONSOLE
DEF myfile:FILE
DEF ln:STRING
IF(OPENFILE(myfile,"C:\CBASICTEST.TXT","R") = 0)
   IF(READ(myfile,ln) = 0)
            PRINT ln                       <----- Here is where you would parse and execute commands found
   ENDIF
   CLOSEFILE myfile
   PRINT "File read successfully"
ELSE
   PRINT "File could not be opened"
ENDIF
PRINT "Press Any Key To Close"
DO:UNTIL INKEY$ <> ""
CLOSECONSOLE
END

[/tt]


If you still don't get it to work, send me (via pm) a sample file and I'll see what I can do to get you started.

Tom
Title: Re: How parse strings from file?
Post by: aurelCB on June 21, 2009, 01:54:27 AM
@Redbolt
Thanks for link and fb code.
This code is all written in functions(declered subs).
I dont know if this way faster or not but i can translate them.

@Tom
Yes that is ok for starting point.
But like i say i have jumps trough current code in richedit.
So my routines execute block of codes.
I will send you Pm with something interesting.
Title: Re: How parse strings from file?
Post by: aurelCB on June 28, 2009, 10:52:53 PM
@REDBOLT you say that you can help in translation .
What a he** is UBOUND,LBOUND ? Same thing has VB.
part of code fbmath:

DIM AS INTEGER Index, L, Lb, Ub

  Lb = LBOUND(Elem)
  Ub = UBOUND(Elem)


Complete code of module string is written in two functions and
will be maby interested for someone else( i think ... ::))
Title: Re: How parse strings from file?
Post by: REDEBOLT on June 30, 2009, 05:55:27 PM
Aurel,

LBOUND and UBOUND are lower and upper bounds, respectively, of an array.
CBasic doesn't support these, nor does EBasic.
The lower bound is always zero.
If you defined an array with n elements, then the upper bound is n-1.

The parse sub is designed to take a string of "words" and stuff them into a
string array.  For example, if the file consists of CSV values, you could
convert each "word" to a number and stuff it into a numeric array.

Here is a small program which builds a string array and then prints them out.


def x[10] as string
def i as int
def  Lb,Ub as int

Lb = 0
Ub = 9

x[0] = "word0"
x[1] = "word1"
x[2] = "word2"
x[3] = "word3"
x[4] = "word4"
x[5] = "word5"
x[6] = "word6"
x[7] = "word7"
x[8] = "word8"
x[9] = "word9"

for i=Lb to Ub
print x[i]
next i

end
[code]
[/code]
Title: Re: How parse strings from file?
Post by: aurelCB on June 30, 2009, 11:29:03 PM
Hi Redbolt...
Thanks i finaly understand what is LBound and UBound  :)