Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with "instr"

Status
Not open for further replies.

Goalie3533

Programmer
Apr 14, 2004
53
US
I'm having a problem with an instr function contained within an if statement.
Here's the line of code giving me trouble:
-------------------
"If id = prodid(i) and not(instr(Prodname(i)),"RENEWAL")) then"
--------------------
For some reason, as long as my "id" is equal to "prodid(i)" then my data is passing into the if statement regardless of whether or not the "instr" portion is true or not.
Is there anything wrong with the way I've set up my instr function?

Thanks.

-Goalie35
 
Yes, it does, so you need to:

If id = prodid(i) and not(instr(Prodname(i)),"RENEWAL") >0) then
 
Zero is Boolean false.
You do not need >0

This should work:

If id = prodid(i) and not(instr(uCase(Prodname)(i)),"RENEWAL")) then

The binary compare will fail if the cases are mixed in the comparison strings.

Make sure they are the same case.

- J
 
Instr Function
Returns the position of the first occurrence of one string within another

Syntax: InStr([start, ]string1, string2[, compare])

The InStr function syntax has these arguments:

start - Optional. Numeric expression that sets the starting position for each search. If omitted, search begins at the first character position. If start contains Null, an error occurs. The start argument is required if compare is specified.
string1 - Required. String expression being searched.
string2 - Required. String expression searched for.
compare - Optional. Numeric value indicating the kind of comparison to use when evaluating substrings. See Settings section for values. If omitted, a binary comparison is performed.

- FateFirst
 
Using > 0 is the correct method. Here is example code:
Code:
str1 = "Hello world!"
str2 = "Hello"

'This is not corect
If True And Not(InStr(str1, str2)) Then
	WScript.Echo "Not in string" ' This is incorrectly output
Else 
	WScript.Echo "Is in string"
End If


str2 = "not"

If True And Not(InStr(str1, str2)) Then
	WScript.Echo "Not in string" ' This is correctly output but it is a coincidence
Else 
	WScript.Echo "Is in string"
End If

str1 = "Hello world!"
str2 = "Hello"

'This is corect
If True And Not(InStr(str1, str2)>0) Then
	WScript.Echo "Not in string" ' This is correctly output
Else 
	WScript.Echo "Is in string"
End If


str2 = "not"

If True And Not(InStr(str1, str2)>0) Then
	WScript.Echo "Not in string" ' This is correctly output 
Else 
	WScript.Echo "Is in string"
End If

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
No, you are mistaken, it is not a 'Coincidence'.

Zero is boolean false.
VBscript makes no determination between 0 and false, they can be use interchangably.

So you can have a fucntion that returns an numeric as a result or 0 as false, although zero can have the duality of meaning 0 or meaning false.





 
I thought in that case you would need to use CBool() to return a valid 'False/else True' expression?

- FateFirst
 
If you are just testing to ensure that the substring exists, you do not need to check the actual return value since anything greater than zero will be true.

However, if you need to determine the position of a substring, you will need to know the return value of instr.

Consider these 2 cases:

Each of these are completely valid in ASP:

Code:
Dim string1, string2
	string1 = "test"
	string2 = "TEST"
	stringMain = "This is a Test"
	
	If InStr(uCase(stringMain), string2) Then
		Response.Write("Found")
	Else
		Response.Write("NOPE")
	End IF
	
	Dim StringA, StringMod
	StringA = "We went to Boston Massachusetts"
	StringMod = Left(StringA, InStr(StringA, "Boston") -1) & Right(StringA, InStr(StringA, "Boston") +1)
	
	Response.Write(StringMod)

Of couse using the replace function would be much more efficient, but this is for demonstrative purposes only.

- J
 
FateFirst,
Not alwsys.

remember, everything is VBScript is actually a variant type.

Consider:
Code:
For x = 0 To 2
		If x = False Then
			Response.Write(x & " = False")
		Else
			Response.Write(x & " = True")
		End If
	Next
 
If it is not coincidence, then why does not using >0 identify str2 as being in str1 whenit is not?


[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Did you remove the uCase()?
Look, this is all the proof you need.

For x = 0 To 2
If x = False Then
Response.Write(x & " = False")
Else
Response.Write(x & " = True")
End If
Next

 
The problem is that fro VBScript, False is equivalent to 0, however True is equivalent to -1 and Instr will return a number >0 when it matches. To use your own style of example:
Code:
For i = 0 To 2
	If i = False Then
		WScript.Echo "False"
	Elseif i = True Then
		WScript.Echo "True"
	Else 
		WScript.Echo "Neither"
	End If
Next

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Right, but I never mentioned anything about checking against a true value.

The for loop I provided was only to demonstrate that 0 is false. I should have used other symantics than true for the output.

If you need to check for a true value this method will not work.

 
So how do you explain that not using >0 gives incorrect results and using it does not?

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
It doesn't give me incorrect results.

This works fine for me:

Dim string1, string2
string1 = "test"
string2 = "TEST"
stringMain = "This is a Test"

If InStr(uCase(stringMain), string1) Then
Response.Write("Found")
Else
Response.Write("NOPE")
End IF

If InStr(uCase(stringMain), string2) Then
Response.Write("Found")
Else
Response.Write("NOPE")
End IF



Give me an example.
 
Look, in the case of inStr, if you are just checking to ensure a substring exists, it is acceptable to not check the specific value is greater than 0.

That is because 0 evaluates to false in Vbscript.
So if inStr returns 0 and 0 == false, and any return value from inStr that is greater than 0 means the substring exists, it is safe to say that in this context 0 = false and > 0 = true.

- J

 
I think the issue is with the [blue]Not[/b]. Run this example:
Code:
x = 0
y = 1

Response.Write  Not CBool(x)
Response.Write  Not CBool(y)

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
I get True and false returned, which is what I would expect.

Do you get a different result?

In VB "Not 1" is
evaluated as True and can be very confusing when you first see it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top