Link to home
Start Free TrialLog in
Avatar of eversoslightly
eversoslightly

asked on

A little variable question :)

Hi!  I am using VB5.

I need to know how to make my program take a look at a variable, and.... if that variable has a cirtain phrase somewhere in it (even in the middle or something), such as Hello! then it'll do something.

Also... I'd like another question answered that has to do with that.  The cirtain catch phrase may be something like (Hello, my name is ????!), and I want it to detect that phrase, and also save to a separate variable the ????.  If you can help, I'd be much apprieciative!  

Thanks a lot!

Both answers are required to get the points, but if you only know one, I'd appriciate if you'd just comment it in, and then  I might give you points separately in another 'fake' question :)
Avatar of DaveD072798
DaveD072798

Can you accomplish this with setting properties? If you code/program is stored in a Class module or an ActiveX DLL then rather than treat it as a "variable" you can treat it as an "object" with specific user-defined properties. Then, if a property is "Hello" or whatever, you can take appropriate actions from any program that recognizes COM objects, including itself.
Avatar of eversoslightly

ASKER

I'm really not familiar with ActiveX, and someother things you commented about here.

Could you please explain?

I'd feel more comfortable (with my knowledge, which is at a beginner to intermediate level :) ) if there was a way to search through a variable for the phrase.... and thus to a if-then statement.

If the way you are trying to explain is the only way, which sounds complicated, I'd appriciate if you'd explain it better, at which I would then raise the points, because it would seem like a harder question :)

Thank's a lot.

-EverSo
Use the INSTR string function for answer one,

For example;
1)
Dim mypos As Integer
Dim SearchString As String
Dim NextVariable As String

'Define what you want to search for.
SearchString = "hello"

'Look for the search string and make it case INsenstive.
mypos = InStr(UCase(Text1), UCase(SearchString))

'We found the search string.
If mypos Then MsgBox "Position of Hello is: " & mypos

2) This one is a little vague given the criteria you gave. Is what you want to search for always going to be after certain words, at the end of a string/sentence, etc? Give me a little more detail on this one and I will tell you how to do it.


Cantrell's method works perfectly well as long as you're doing everything within a single program. I'd recommend it if that's what you're attempting. I probably read more into your question. If you're trying to talk between applications or objects, you'd probably need to use properties, or at least methods (Public Functions and Public Subs). If all you're doing is working within a SINGLE app that try Cantrell's answer. Sorry for the waste of bandwidth!
Question 2: Just to make sure that I got the question right:

You would like to read a sentence into a variable and detect certain keywords (or sentences). If that keyword, or sentence, is present, you would like to extract certain parts of the sentence and put these parts into a separate variable.

That's an interesting problem which is not quite that easy to solve. Basically, you would need to write a price of software called "parser" that extracts the elements of a sentence and finds the certain variables.

For an easy start, try the following (the following is not verified with VB, so there might be a few syntax errors):

Function FindName(sInputLine as String, sOutName as String) as Boolean

Const cSSBegin = "Hello! My name is "
Const cSSEnd = "!"

Dim iCurSearchPos as Integer
Dim iPrevSearchPos as Integer
Dim iKeyStart As Integer
Dim iKeyEnd as Integer

' default result
FindName = False

' first, try to find cSSBegin:
iCurSearchPos = InStr(UCase(sInputLine), UCase(cSSBegin))
if iCurSearchPos = 0 then ' not found
  Exit Sub
Endif

iKeyStart = iCurSearchPos + Len(cSSBegin)

' Find the end of the sentence:
' Search the LAST occurence of cSSEnd in the string
iCurSearchPos = 0
iPrevSearchPos = 0
While iCurSearchPos <> 0
  iPrevSearchPos = iCurSearchPos
  iCurSearchPos = InStr(iPrevSearchPos + 1, UCase(sInputLine), UCase(cSSEnd))
Wend

If iPrevSearchPos = 0 Then  'end not found; assume rest of string
  iKeyEnd = Len(sInputLine)
Else ' end found
  iKeyEnd = iPrevSearchPos
endif

' not extract the result; may require more checking
sOutName = Mid$(sInputLine, iKeyStart, iKeyEnd-iKeyStart)
FindName = True
End Function

To use:
  Dim sName as String
  If FindName(Text1.Text, sName) Then
    MsgBox "Hello " & sName & "!"
  Endif


Thanks for your help!

I don't have time to check these out right now... I'll check em out tonight sometime....

but...

(I was in kind of a hurry when writing it out, soo.... to clarify)


I've got a variable.

It may be something like "Whatever whatever talking garble yada yada Hello!  My name is Bob.  How are you? yada yayda whatever"

Now.... I want some sort of an if then statement....

If, somewhere in this variable is "Hello!  My name is ?????.  How are you?"  Then do..... whatever....

Also, then, as in question 2, I'd want it to save 'Bob', or whatever the ????? might happen to be, to a separate variable.

Hope this clarifys it.... feel free to ask more questions... I'll try these out tonight :0

-Ever-So
You might want to check for Eliza-Source Code. There is a basic version around, I believe. Eliza (Elisa?) is an Pseudo-AI that recognises certain keywords and reacts accordingly.
ASKER CERTIFIED SOLUTION
Avatar of joe_s
joe_s

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok... the results of my testing :)

fulscher had the right idea, yet it didn't work.

cantrell's thing for question was right on the numbers.  I'd like to give him points, which I will in a separate question... keep and eye out for that cantrell! :)

And joe_s - regarding question 2, with what you put, it works... I'd like it if you could write out that what you said with the left$.  Then I'll give you the points.

Thanks!
This will do it if you know what the character after the name is. The second example will work for any non-numeric character following the name. Good Luck.

Dim sLookingFor As String
Dim iFoundPosition As Integer
Dim sLookingInString As String
Dim sName As String

sLookingInString = "abcdefg Hello, my name is Fred. What is your name?"
sLookingFor = "Hello, my name is"

iFoundPosition = InStr(1, sLookingInString, sLookingFor)
sName = Right$(sLookingInString, Len(sLookingInString) - iFoundPosition - Len(sLookingFor))

MsgBox "Right side of string is :" & sName & ":"

iFoundPosition = InStr(1, sName, ".")
sName = Left$(sName, iFoundPosition)

MsgBox "Name is " & sName

---------------------------------------------
This code will find "Fred" as long as the character following 'd' is not alpha:

Dim sLookingFor As String
Dim iFoundPosition As Integer
Dim sLookingInString As String
Dim sName As String
Dim sChar As String
Dim iIndex As Integer

sLookingInString = "abcdefg Hello, my name is Fred What is your name?"
sLookingFor = "Hello, my name is"

iFoundPosition = InStr(1, sLookingInString, sLookingFor)
sName = Right$(sLookingInString, Len(sLookingInString) - iFoundPosition - Len(sLookingFor))

MsgBox "Right side of string is :" & sName & ":"
iIndex = 0
Do
  iIndex = iIndex + 1
  sChar = UCase$(Mid$(sName, iIndex, 1))
Loop While (iIndex < Len(sName) And Asc(sChar) > 64 And Asc(sChar) < 91)

sName = Left$(sName, iIndex - 1)

MsgBox "Name is " & sName


Thanks!

It worked great.