Let's get down to bid-ness!
Bid-ness is programming, and Bid-ness is good, am I right? Anyway, I'll start by giving the code that was show in the last tutorial.
Declare Function Parse(ToParse as String)
Dim Shared Parsed(100) as String
Function Parse(ToParse as String)
Dim CurrentPosition as Integer
Dim CurrentCharacter as String
Dim WordCount as Integer
Dim WordSize as Integer
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 CHR$(34), "'", ","
If Not WordSize = 0 Then
Parsed(WordCount + 1) = MID$(ToParse, CurrentPosition - WordSize, WordSize)
End If
WordSize = 1
Parsed(WordCount + 1) = MID$(ToParse, CurrentPosition - WordSize + 1, WordSize)
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 Function
Ooh… It's pretty… What is it? It is a text parser. It's whole purpose is to take some text and break it up into pieces to make it eiser to work with.
"What kind of text, oh writer, might be made easier to work with?"
Well, anything really… Not specific enough? Fine how about this? Ever heard of a scripting language? If not, there are some great tutorials on the net that talk about them. A scripting language is a great way to make your game engine flexable and reusable. If you wrote a game and made it so that all events in the game were hard coded into the engine, it would make that code extremely difficult to use in a future project. By using a scripting language, you make it far more easy to use again. Observe…
1. Include "parser.bas"
Dim CommandString as String Dim I as Integer
'open a text file containing your commands. Open "commandfile.txt" For Input as #1 Do
'Reads you commands from the file one line at a time
Input #1, CommandString
Parse(CommandString)
'This Select is for all the commands you want to make plus one
'to tell you you misspelled one of your commands some where.
Select Case Lcase(Parsed(1))
Case "print"
For I = 2 to CVI(Parsed(0))+1
print Parsed(I) + " ";
next I
print
Case else
'This lets you know that you messed up somewhere
Print Parsed(I) + " is an Invalid command!"
End Select
Loop Until EOF(1)
close #1
sleep
This is an extremely simple example, but it can be tweaked to do what ever you what. This works best for files that don't have comma separated lists. If that's what you're using it's better to just read them into separate variables like normal. But when working with a plain text file, this works wonders. That's not all it can do, either.
It slices! It dices!
Okay, so maybe it's no good for Julianne fries, but it does have a number of uses. I originally wrote this parsing routine for use with an AI Chatter bot. For those of you who don't know, a chatter bot is just a program that tries to talk to the user. I would love to beable to post working code for this example, but my own project is unfinished. The sky is pretty much the limit when it come to this routine.
You could use it for reading some other programs file type. (assuming you know the file structure) You could use it for a scripting engine, as seen above. You could use it to dissect user input as show in the first tutorial. You could use it to take over the work… Okay maybe not that one.
The main thing to remember is that the parser routine is not set in stone. You can change it to fit any needs you might have by adding or modifying the Case statements. You can change the symbols and characters that the parser is looking for to extend it to do what ever you wish.
Conclusion
I hope this has been informative. I can't think of anything else to put with it… So… Get outta here… Scat… Don't make me get the hose…
Imortis Inglorian