Instr$ Question

Blitz3D Forums/Blitz3D Beginners Area/Instr$ Question

QuickSilva(Posted 2004) [#1]
Please could someone tell me why the following will not work.

;=============================================

a$="AAAABBBBBBBBFFFFFFDDDDDDABCAABB"

Select True
Case Instr(a$,"ABC")
END
End Select

;=============================================

Shouldn`t this end the program as ABC is contained in the string a$?

The following works fine but I`m curious...

;=============================================

a$="AAAABBBBBBBBFFFFFFDDDDDDABCAABB"

If Instr(a$,"ABC") Then End

;=============================================

Regards,
Jason.


Michael Reitzenstein(Posted 2004) [#2]
Instr returns the position at which the substring occurs, and True evaluates to 1 when used in this way. Try RuntimeError Instr( "AAAABBBBBBBBFFFFFFDDDDDDABCAABB", "ABC" ) and RuntimeError True to confirm this.


JazzieB(Posted 2004) [#3]
Actually, any value other than 0 is taken to be True (positive or negative). Strange that it doesn't work this way in Select..Case constructs. To get around it in this example just stick ">0" on the end of "Instr(a$,"ABC")" so it becomes "Case Instr(a$,"ABC")>0".


PowerPC603(Posted 2004) [#4]
It appears that the "IF"-command considers everything that isn't "0" as "True".

Try this code:
If 5 Then
	Print "IF-command sees TRUE"
Else
	Print "IF-command sees FALSE"
EndIf


You can experiment with the value ("5" in the example code) and set it to "0" and see what it does to the IF-statement.

The CASE-command is only True if the parameter in an exact match.

As Michael said: Instr returns the position at which your string ("ABC") appears in the string you want it to search in (a$).
Therefore the IF-statement accepts this position (which isn't "0") as True, and the CASE doesn't match the TRUE parameter you've set by the SELECT statement (True = 1).

What you can do with the Select-statement:
a$="AAAABBBBBBBBFFFFFFDDDDDDABCAABB"

Select True
	Case Instr(a$,"ABC") > 0
		End
End Select


This way the CASE-statement chechs the comparison between the value returned by "Instr" and "> 0", which is "True".

[Edit]
It seems that JazzieB had the same idea as I did and we were typing at the same time. lol


QuickSilva(Posted 2004) [#5]
Thanks, that cleared it up nicely :)

Jason.


Michael Reitzenstein(Posted 2004) [#6]
Actually, any value other than 0 is taken to be True (positive or negative).

Nope, the constant True is interchangeable with 1, as can be seen in this example:

If 5 = True

	RuntimeError "Yes"
	
EndIf

If 5 

	RuntimeError "No"
	
EndIf



JazzieB(Posted 2004) [#7]
I wasn't talking about the True constant, I was refering to what values are interpreted as true. Anyhoo, problem solved.