Posted: Sat Mar 18, 2006 9:34 pm Post subject: String Manipulation
Is there an easy way to identify a specific word within a string? You see, I want the program to identify words such as "take" or “key” within a sentence no matter its placement.
If this is possible, then a person could type variations of the same sentence and get the same result. For example, whether you type “takekey”, “take the key”, or “Well, I see that key sitting on the ground. Perhaps I will take it.” you will get the same message saying, “you have taken the key.”
PS: Is it possible to have a string variable represent more than one value? Could it represent “take”, “get” and “pick up” all at once?
Thank you for your time.
Sponsor Sponsor
person
Posted: Sat Mar 18, 2006 9:45 pm Post subject: (No subject)
maincharname wrote:
Is there an easy way to identify a specific word within a string? You see, I want the program to identify words such as "take" or "key" within a sentence no matter its placement.
If this is possible, then a person could type variations of the same sentence and get the same result. For example, whether you type "take key", "take the key", or "Well, I see that key sitting on the ground. Perhaps I will take it." you will get the same message saying, "you have taken the key."
I don't know if this would be the best solution, but you could sort all of the words in a sentence in an alphabetic order. For example.
"It is a nice day today."
would be sorted to:
"a day is it nice today."
You could then perform a search to see if the strings that you are looking for are present in the sentence.
maincharname wrote:
PS: Is it possible to have a string variable represent more than one value? Could it represent "take", "get" and "pick up" all at once?
Yes. Look up "Records".
maincharName
Posted: Sat Mar 18, 2006 10:04 pm Post subject: (No subject)
Thank you, that helps a little. But how do I create a search?
Tony
Posted: Sat Mar 18, 2006 10:05 pm Post subject: (No subject)
index() is your friend - the function finds a substring in a larger string and returns its location.
so
code:
if index(" your sentance here ", " key ") > 0 then
% something about a key
it is important to have spaces surrounding your word, otherwise other words such as 123key456 will produce an unwanted result. As such, you also need to add spaces to beginning/end of your sentance (to check for first/last word) and lower/up-case everything, as "Key" is not the same as "kEy"
Posted: Sat Mar 18, 2006 10:32 pm Post subject: (No subject)
Same as in the one that worked: take the key
[Gandalf]
Posted: Sat Mar 18, 2006 10:37 pm Post subject: (No subject)
code:
get :*
This allows you to have spaces in your input. It will allow anything up to 255 characters in length.
code:
var answ : string
get answ :*
if index(answ, " key ") > 0 then
put "you have taken the key"
else
put "sorry, what?"
end if
This will not work because it is searching for a space after "key", which it will not find because your string ends at 'y'. Remove this space and it will work correctly.
Sponsor Sponsor
Tony
Posted: Sat Mar 18, 2006 10:39 pm Post subject: (No subject)
maincharName wrote:
Same as in the one that worked: take the key
no it's not
code:
var answ : string
get answ
% "take the key"
put answ
% "take"
hmm... perhaps you need to read the whole sentance, and not just the first word
code:
var answ : string
get answ:*
put answ
edit: in a sentance such as "I am keyless" the program will think that you have a key. So just
Posted: Sat Mar 18, 2006 10:41 pm Post subject: (No subject)
Tut, tut.
What I find most interesting is that he had a post after:
Quote:
Same as in the one that worked: take the key
Which explained that he removed the spaces from the string, or something along those lines.
maincharName
Posted: Sat Mar 18, 2006 10:43 pm Post subject: (No subject)
maincharName wrote:
It works perfectly when the text is predefined:
code:
var answ := "take the key"
if index(answ, "key") > 0 then
put "you have taken the key"
else
put "sorry, what?"
end if
But what did I do wrong here?
code:
var answ : string
get answ
if index(answ, "key") > 0 then
put "you have taken the key"
else
put "sorry, what?"
end if
Sorry about that, I'm a bit tired. To clerify, the first code didn't work when there was a space before and after the word key in quotations: " key "
so I changed it to: "key"
Unfortunately, the...
code:
var answ : string
get answ
...still doesn't work
Tony
Posted: Sat Mar 18, 2006 10:43 pm Post subject: (No subject)
[Gandalf] wrote:
Quote:
Same as in the one that worked: take the key
Which explained that he removed the spaces from the string, or something along those lines.
It also tells me that he hasn't bothered to check what the actual value was, because clearly it is not the same
Posted: Sat Mar 18, 2006 10:55 pm Post subject: (No subject)
What? Ok, now I'm getting frustrated. Please just try it yourself if you don't believe me. Please copy the following code into turing, run it and type, "take the key" (without the quotations of course) and see whether it gives you the same results as the predefined version.
code:
var answ : string
get answ
if index(answ, "key") > 0 then
put "you have taken the key"
else
put "sorry, what?"
end if
I actually do care and have had turing open the whole time.