Results 1 to 4 of 4

Thread: Parsing a File one line at a time.

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2000
    Posts
    10

    Question Parsing a File one line at a time.

    Here's the situation.

    I load a text-based file into an invisible TextBox.(Lets call it TextBox1). I then need to read the information from TextBox1 one line at a time, then pass that line to another invisible TextBox (TextBox2), where i process the information in that line. Then i need to be able to jump to the next line in TextBox1 and repeat this till the last line in TextBox1.

    Fine i thought, use Split with ChrS(13) and then just increment the the number to read the next line...

    This works *basically* but is not what i want to achieve.

    Maybe i should go into a little detail.... (Watches everyone click the back button )

    I am making my own client for a game which already has a scripting language invented for it (Real easy language)

    Now i want to be able to use the existing scripting language, and scripts, with my client. So in other words i'm just making an interpreter between the client and the script.

    Now as i stated above, i need to load the script up, parse it 1 line at a time, and then process that line befre moving onto the next line.

    One of the major commands of this script language gets in the way though, lemme show ya a small example of the script language so as to better understand.

    ---Snip!---

    Put Look
    Match SEEBOB You see Bob
    Match SEEFRANK You see Frank
    Match SEEJOE You see Joe
    Matchwait

    SEEBOB:
    *more commands related to Bob*

    SEEFRANK:
    *more commands related to frank*

    SEEJOE:
    *more commands related to joe*

    --Snip!--

    The Put command simple sends everything after it to the game, just as you would type it (eg. 'put look' sends 'look' to the game)
    Interpreting put should be no problem, it the next bit...
    The Match command works like this...
    Syntax : MATCH <subtogoto on succesful match> <text to match>

    So 'MATCH SEEBOB You see Bob' Would take me to the Sub defined as 'SEEBOB:' when the 'You see Bob' text appears in the actual viewing screen (RichTextBox1 for arguments sake).

    By the way, the MATCHWAIT command simply waits until one of the above MATCHes is met.

    You can probably see my problem now. I need to parse the Text from the Script line by line, process it, then continue onto the next line. But if i bump into a match command i need to make variables from the 2 Strings after it, then wait for a MATCHWAIT before i process it, then jump to the Corresponding Section on a successful match (Much like VB command Goto)

    *Scream!*

    Any help on this would be most appreciated, as I wrote down a rough idea of what i needed to do, blinked, then see above section marked *Scream!*

    There's gotta be some 'not-as-longwinded-as-it-looks' type code that could be made to do what i wish.

    Thanks for any help on this topic, and sorry for having such a longwinded post, but i like to explain EXACTLY what i am looking for before asking anyone for help.

    Regards,

    David Perry

    VB6 Enterprise SP4


  2. #2
    Member
    Join Date
    Aug 2000
    Posts
    60

    Talking Solution (I Hope)

    I hope that this will solve your problem, at least it will be if i have read your description properly.

    Refering to the Match command:

    Example line: Match SEEBOB You see Bob

    Ok, so I take it you have figured out how to read the line, you can then use instr to find the command:

    if Instr(1, lcase$(theline), "match") then
    'This is where you process the rest of it
    end if

    The code above changes the case of the line being processed (theline) to lower, so that it does not matter whether the user has entered it in lowercase or uppercase or a mixture. The Instr command returns the position of the second string, in this case "match", so if it is not equal to 0 then the command does exist.

    ---

    Right, now down the the bit you really wanted to know:

    Using the same example line as above.

    Here we have to set one basic rule, no spaces in the labelname eg. "SEEBOB"

    Once you have set this rule it becomes plain sailing, all you have to do is use the instr command again. This command allows you to search for strings inside another string, but it also allows you to specify what character position it start looking from. So all we have to do is find the first space (character code: 32) then from that point find the second one, so now we know that the label lies between the first space and the second space. And the other bit lies between the second space and the end. So from this you can deduce the code as below:

    Dim FirstSpace, SecondSpace As Long
    Dim theline, FirstVariable, SecondVariable As String

    theline = "Match SEEBOB You see Bob"
    'theline is the line that we are processing
    FirstSpace = InStr(1, theline, Chr$(32))
    SecondSpace = InStr(FirstSpace + 1, theline, Chr$(32))

    'Get the first variable from the string
    FirstVariable = Mid$(theline, FirstSpace + 1, SecondSpace - FirstSpace)

    'Get the second variable from the string
    'You could use Right$ for this, but it requires more work

    'PS Below is only one line, at the time of writing it had
    'spanned onto two lines
    SecondVariable = Mid$(theline, (SecondSpace + 1), Len(theline) - SecondSpace)


    Hope this is what you were looking for...

    I have tested this code and it does work. If you have any problems please e-mail me @ [email protected]
    Grant French
    -----------------------------------------------
    E-Mail: [email protected]
    ICQ: 33122184

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2000
    Posts
    10

    Thumbs up

    Thanks, It's a great Start to what seems to me to be a long road.

    The major problem i am having with the MATCH thing is that there could be (in theory) an infinite amount of MATCHes before the MATCHWAIT, although I've never seen more than 10-15 used, nor should there be any need to use more.

    So really, i need to be able to use this function repeatedly on each match to get the 2 variables, keep them stored seperately, then on a MATCHWAIT i need to parse the incoming info from the server looking for the first occurance of any of the second variables. After a success on one of the MATCHES I then need to find out which MATCH was met, get the firstvariable for said MATCH, then jump to the reference point for that MATCH in the Script File and continue running it.

    Wow, that looks awful reading it, lemme try this way...
    1 2
    --Script--
    MATCH BOB You see Bob (Stores BOB in Var1, You... in Var2)
    MATCH FRED You see Fred (FRED in (a different?)Var1, You see Fred in Var2
    MATCH JOE You see Joe (JOE in (3rd)Var1, You see Joe in Var2)
    MATCHWAIT (Starts parsing the incoming data for any of the above named Var2's)
    (Simulating a succesful find for You see Fred)
    (Simulating the function that links Var2 with Var1, therefore making You see Joe = JOE)
    (Simulating Converting JOE to JOE: and searching for line in said script, then beggining parsing script from line after that)
    BOB:
    *some commands*
    FRED:
    *some other commands*
    JOE: (Script jumps to here)
    *Some commands* (Parsing of script begins here again, because of succesful MATCH on Joe)
    --Script--

    NB. All stuff in brackets or marked with *'s above are only notes to show how it works and what I wish to do.

    Thanks for the reply though, much appreciated, and at this point, any help is better than none


    Perhaps a good way for me to start would be to figure out how to parse some text in a textbow into another textbox, proces the line of text, then load the next line into the textbox.

    Regards,

    --David Perry

    PS. A fellow UKer too i see, and on BTInternet, I used BT for ages, till i managed to get Libertysurf(24/7 free access for 12 months for a whole £55...*drools*).

  4. #4
    Member
    Join Date
    Aug 2000
    Posts
    60

    Hello Again

    I also have switch to Libertysurf 24/7, but i still use my BT Mail account, but i got libertysurf for 13months because of their cockups.

    You can use a custom type array to store all of the information about the different match commands e.g


    Example Lines: MATCH BOB You see Bob
    MATCH FRED You see Fred

    --Put this in a module--

    Public Type MatchItemsType
    StringToMatch as String
    Var1 as String
    Var2 as String
    End Type

    'Replace 30 with however many you want to store
    Global MatchItems(30) as MatchItemsType



    --The code behind the form--

    MatchItems(0).Var1 = "BOB"
    MatchItems(0).Var2 = "You see Bob"

    MatchItems(1).Var1 = "FRED"
    MatchItems(1).Var2 = "You see Fred"

    'You get the idea, right?


    Then when the user enters say; You see Bob
    You could then do a loop as below to find which (if any) of the MatchItems contain the string that has been entered by the user.


    'Again change 30 to the most you want to be able to handle
    For MatchLoop = 0 to 30

    If InStr(1, UserInput$, MatchItems(MatchLoop).Var2) then

    'The string that the user has entered has been found
    'Put your code here to goto the Label stored in:
    'MatchItems(MatchLoop).Var1

    End If

    Next MatchLoop

    'After that you need to blank them all ready for next time
    For MatchLoop = 0 to 30

    MatchItems(MatchLoop).Var1 = ""
    MatchItems(MatchLoop).Var2 = ""

    Next MatchLoop



    Again I hope this helps you, If you want more help get ICQ from http://www.icq.com, i think it is about 3Mb

    My ICQ Number is 33122184

    Mail Me @
    [email protected]
    or if you like; [email protected]
    Grant French
    -----------------------------------------------
    E-Mail: [email protected]
    ICQ: 33122184

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width