September 26, 2020, 06:37:30 pm

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


How parse strings from file?

Started by aurelCB, June 19, 2009, 08:28:40 am

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

aurelCB

June 19, 2009, 08:28:40 am Last Edit: June 19, 2009, 08:30:37 am by aurelCB
Hi...
First of all i search trough forum and find only Quentin explaination about parsing
Code Select
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

Code Select
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
forum for hobby programming:<br />https://aurelsoft.ucoz.com

tbohon

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
"If you lead your life the right way, the karma will take care of itself ... the dreams will come to you."  -- Randy Pausch, PhD (1961-2008)

aurelCB

June 19, 2009, 01:01:43 pm #2 Last Edit: June 19, 2009, 01:05:23 pm by aurelCB
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?
forum for hobby programming:<br />https://aurelsoft.ucoz.com

REDEBOLT

Download the fbmath library.  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.
Regards,
Bob

tbohon

From the CBasic user's guide start with something like this:

Code Select
[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
"If you lead your life the right way, the karma will take care of itself ... the dreams will come to you."  -- Randy Pausch, PhD (1961-2008)

aurelCB

@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.
forum for hobby programming:<br />https://aurelsoft.ucoz.com

aurelCB

@REDBOLT you say that you can help in translation .
What a he** is UBOUND,LBOUND ? Same thing has VB.
part of code fbmath:

Code Select
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 ... ::))
forum for hobby programming:<br />https://aurelsoft.ucoz.com

REDEBOLT

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.

Code Select

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]
Regards,
Bob

aurelCB

Hi Redbolt...
Thanks i finaly understand what is LBound and UBound  :)
forum for hobby programming:<br />https://aurelsoft.ucoz.com