Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / results of FIND FIRST CHAR$(source, char)

Author
Message
zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 11th Oct 2011 01:57 Edited at: 11th Oct 2011 02:05
I'm working with the built-in string commands rather than utilizing "matrix1utils" plugin for demonstration purposes. I ASSUMED that if the char wasn't found in the source; the command would return either 0 (but I can see that can be confusing given arrays and arrays of char's starting indexes) or -1. As it turns out, I get large varying results (though they all seem to be negative in my tests) depending on the size of the string.

Can someone ASSURE me of two things.
1. That if the char is not found it the source that the result will always be negative.

And

2. That I am not opening up a potential disaster of returning some out of range negative number that could cause a crash.

Right now I am handling the result by checking against a negative value, or (just in case) a positive value that is higher than the length of the source string.



If the char isn't found in the source, my results for split typically wind up being around -49877375.

Your signature has been erased by a mod please reduce it to 600 x 120.
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 11th Oct 2011 03:46
If it were me, I would do something like this:



to check to see if there is a space in the string. I got a negative value each time I tried it, but I don't know if it always will.

zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 11th Oct 2011 10:53 Edited at: 11th Oct 2011 10:57
Quote: "if mid$(input$,i,1) = " " then okay = 1
"


Unfortunately, I believe the built-in dbpro MID$ command doesn't take 3 parameters which is generally more common and useful. Unless it's undocumented???

I know IanM's 'matrix1utils' has that type of MID$, but I wanted to avoid any 3rd-party-dll's for this demonstration.

I'm sure I could work my own proper MID$ function, but I jumped on the FIND FIRST CHAR$ command as it 'seemed' to do what I needed.

Your signature has been erased by a mod please reduce it to 600 x 120.
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 11th Oct 2011 11:14 Edited at: 11th Oct 2011 11:26
Just remove the ,1 in the MID$() command and it'll use the native Darkbasic command that only takes out one character at a time.

FIND FIRST CHAR$() does show a negative number if it doesn't find the search character in the string so it seems to work fine.

I changed input$ to Tex$ since INPUT is a command and added the LEN() command since I didn't see it define input_string_length.



I'm curious why you want take out the first 5 characters if the first space in the string is non-existent or more than or equal to 5. The only time it takes into account the space is when the first space or length of the string is less than 5. Why bother with checking where the space is if 99% of the time it's going to take out the first 5 characters for cmd1$ and the next 5 characters for cmd2$?

zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 11th Oct 2011 11:29 Edited at: 11th Oct 2011 11:50
@ LBFN, thanks for your suggestion too. Just to be on the safe side I may reimplement this combining your suggestion and Grueslayer's

@ Grog Grueslayer, Thanks for the suggestions and the example source.
.
Quote: "I'm curious why you want take out the first 5 characters if the first space in the string is non-existent or more than or equal to 5. The only time it takes into account the space is when the first space is less than 5. Why bother with checking where the space is if 99% of the time it's going to take out the first 5 characters for cmd1$ and the next 5 characters for cmd2$?"


The reason is... The code snippet is part of a Text Adventure game that I am converting from Microsoft BASIC to DBpro. The way the parser/command tokenizer is implemented... it disregards anything after the first five characters. So if the user types "inventory", the parser simply ignores the "tory" and can tokenize the command based on inven. There are no commands in the vocabulary which can not be distinguished from another,, passed 5 characters max limit. Thus I can allow the user to type anything beyond inven and it will be truncated to "inven". I like this method, as there is no need to force users to type lenghty commands. And it saves me from having to type in a lot of extra characters in my verb and object lists, for no good reason.

I think this helps answer the second part of your question. It's important, because I need to make sure I get those 5 characters from the first word.... if there is a second word, I need to get those 5. But... In both cases the verb and object could also contain less than 5 characters. like the command "Go Up". Also, I need to read that even if the words typed in by the user are longer than 5 characters. So "Go Up" == "Go Upstairs". It gets a bit more complex as the tokenizer equates similar verbs like "Take" == "Grab" == "Get" == "Fetch" == "Retrieve" etc...


Quote: "The only time it takes into account the space is when the first space or length of the string is less than 5"

Not really. Unless I made a logical error. I will have to check. It's meant to get up to the first 5 characters of the first word (or less), AND up to 5 characters (or less) of the second word; if there is a second word.

Your signature has been erased by a mod please reduce it to 600 x 120.
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 11th Oct 2011 12:06 Edited at: 11th Oct 2011 12:38
Ah, may I suggest then that you automatically add a space to the end of the string (just in case the user doesn't add a space). Then use the space to extract everything left of the space as cmd1$ and anything after the space as cmd2$... then cut the strings down to 5 characters.

In this code snip the cut down strings are c1$ and c2$ to show what it's doing but when you want that to work without showing the strings spit at the space just change c1$ to cmd1$ and c2$ to cmd2$.



One thing you may try is something I did for one of my fist text adventures in QuickBasic. I extracted the first word and the last word to kinda fool the user to think the text parser was more advanced than it actually was. Like if the user typed "GET THE BLOODY BAT" it would extract "GET" and "BAT". "GO TOWARDS THE NORTH" would be "GO" and "NORTH". It worked pretty well.

zenassem
21
Years of Service
User Offline
Joined: 10th Mar 2003
Location: Long Island, NY
Posted: 11th Oct 2011 13:16 Edited at: 11th Oct 2011 13:25
Thanks Grog,, I'll look it over and consider it, because I do plan on adding functionality to the parser. My intention was to nail down a simple verb/object parser, "complete" with a simple game. Using it as a code snippet example/tutorial on Text Adventure/Parser-tokenizers.

Then I'll build on the basics to include ignoring "filler/noise" words like "a", and "the"; as well as the addition of parsing indirect-objects or prepositional phrases.

The parser would handle 3 predicate parts; verb, the direct object, and the indirect object or Prepositional phrase.

Finally, I would handle adjectives to modify nouns, and adverbs to modify verbs.

******

An example from the text I'm using ('Compute!'s Guide to Adventure Games', ©1984) would parse the following... (with the assumption that nothing precedes the verb; except perhaps... a noun of address)

"GIVE THE LITTLE PIG TO THE BIG BAD WOLD QUICKLY"
* The verb (GIVE), modified by an adverb (QUICKLY)
* The object (PIG), modified by one adjective (LITTLE)
* The indirect object (WOLF), related to the verb by a preposition (TO) and modified by two adjectives (BIG and BAD).

so in BNF Form:


That's about as far as I will take the parser/tokenizer. After the first example I will make use of IanM's matrix1utils to ease the burden.

Your signature has been erased by a mod please reduce it to 600 x 120.
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 12th Oct 2011 05:39
Sounds like you're on the right track.

The ultimate goal for any text adventure game is to try to mimic Infocoms text parser. It allowed multiple commands on the same line. The last one I programmed separated each command when it saw "then" and a period. Every time it saw a comma or "and" it assumed the last command should be used like "TAKE THE BAT, BALL, AND GLOVE". I've always had a lot of fun making text adventures but never quite finished any of them 'cause my goals were always to make them as advanced and massive as Zork.

LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 12th Oct 2011 05:53
Speaking of Zork, wasn't there a Grue in there somewhere Grog?

Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 12th Oct 2011 07:46
Yeah, Grues showed up to kill the player anytime they wandered around in the dark too long. If you went into an unlit area it would say:

It is pitch black. You are likely to be eaten by a Grue.

3 more moves in darkness and you're lunch.

My last name was taken from Zork.

Grueslayer was a legendary blade used by Entharion the Wise to slaughter countless Grues. It was first mentioned in Beyond Zork where you could see the Sheath of Grueslayer (not the blade) in the weapons shop. I've always loved all the Zorks even the not so cool graphic only ones.

Login to post a reply

Server time is: 2024-11-24 01:32:07
Your offset time is: 2024-11-24 01:32:07