
-----------------------------------
maincharName
Sat Mar 18, 2006 9:34 pm

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 &#8220;key&#8221; 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 &#8220;take key&#8221;, &#8220;take the key&#8221;, or &#8220;Well, I see that key sitting on the ground. Perhaps I will take it.&#8221; you will get the same message saying, &#8220;you have taken the key.&#8221;

PS: Is it possible to have a string variable represent more than one value? Could it represent &#8220;take&#8221;, &#8220;get&#8221; and &#8220;pick up&#8221; all at once?

Thank you for your time.

-----------------------------------
person
Sat Mar 18, 2006 9:45 pm


-----------------------------------
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.

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
Sat Mar 18, 2006 10:04 pm


-----------------------------------
Thank you, that helps a little. But how do I create a search?

-----------------------------------
Tony
Sat Mar 18, 2006 10:05 pm


-----------------------------------
index() is your friend - the function finds a substring in a larger string and returns its location.

so

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"

-----------------------------------
maincharName
Sat Mar 18, 2006 10:27 pm


-----------------------------------
It works perfectly when the text is predefined:

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?

var answ : string
get answ
if index(answ, " key ") > 0 then 
  put "you have taken the key"
else
  put "sorry, what?"
end if

-----------------------------------
Tony
Sat Mar 18, 2006 10:29 pm


-----------------------------------

var answ : string
get answ 

what's the value of answ?

-----------------------------------
maincharName
Sat Mar 18, 2006 10:32 pm


-----------------------------------
Same as in the one that worked: take the key

-----------------------------------
[Gandalf]
Sat Mar 18, 2006 10:37 pm


-----------------------------------
get :*
This allows you to have spaces in your input.  It will allow anything up to 255 characters in length.

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.

-----------------------------------
Tony
Sat Mar 18, 2006 10:39 pm


-----------------------------------
Same as in the one that worked: take the key
no it's not


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 :wink: 

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

answ := " " + answ + " "

to add space buffers to beginning and end

-----------------------------------
[Gandalf]
Sat Mar 18, 2006 10:41 pm


-----------------------------------
Tut, tut.

What I find most interesting is that he had a post after:
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
Sat Mar 18, 2006 10:43 pm


-----------------------------------
It works perfectly when the text is predefined:

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?

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...
var answ : string
get answ
...still doesn't work

-----------------------------------
Tony
Sat Mar 18, 2006 10:43 pm


-----------------------------------
"]
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 :?

-----------------------------------
maincharName
Sat Mar 18, 2006 10:45 pm


-----------------------------------
Yes, it was the same! I just neglected to completely paste the code. That problem was in the forum, not the code.

-----------------------------------
Tony
Sat Mar 18, 2006 10:49 pm


-----------------------------------
That problem was in the forum, not the code.
Unbelivable..

var answ : string
get answ %enter "take the key"
put answ %what does it show?

If you're going to tell me that the value of answ is "take the key", you'd have to upload a screenshot.

-----------------------------------
maincharName
Sat Mar 18, 2006 10:55 pm


-----------------------------------
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.

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.

-----------------------------------
Tony
Sat Mar 18, 2006 10:56 pm


-----------------------------------
show me a screenshot with answ's value.

-----------------------------------
maincharName
Sat Mar 18, 2006 11:01 pm


-----------------------------------
and here is your screen shot, I know it's small but you can clearly see that it says "sorry, what?" and not "you have taken the key"

-----------------------------------
Cervantes
Sat Mar 18, 2006 11:02 pm


-----------------------------------
"]get :*
This allows you to have spaces in your input.  It will allow anything up to 255 characters in length.
Actually, it allows more than 255 characters, doesn't it? Which, of course, causes an error. But the point is, it won't stop you at 255 characters, as
get : 255
will.

IIRC.

-----------------------------------
Tony
Sat Mar 18, 2006 11:06 pm


-----------------------------------
you can clearly see that it says "sorry, what?" and not "you have taken the key"
you're talking about the outcome of your if statement. I was asking you for
show me a screenshot with answ's value.
value of answ variable, so that you can see why the if statement makes the logical decision as it does

-----------------------------------
[Gandalf]
Sat Mar 18, 2006 11:06 pm


-----------------------------------
Cervantes, Cervantes...  Of course, you are right...  I meant that it will not allow you to continue with the program, causing a run time error.

maincharName, why are you not using get :*?  The code you most recently posted will say "Sorry, what?" because the only word which is put into answ variable is "take".  Use get :* to solve this.

-----------------------------------
maincharName
Sat Mar 18, 2006 11:17 pm


-----------------------------------
thank you Gandalf! 

I'm sorry I missed that post. It's getting late and it was just above my half closed eye lids.

Problem solved! Thank you everyone!
