|
Post by GatorDog on Jul 22, 2011 4:32:46 GMT 1
What am I missing here I want to execute the CASE statement based on the presence of Needle$ in Haystack$. x$ = "123abc456"
' PRINT works PRINT INSTR(x$, "abc")
' This doesn't SELECT x$ CASE INSTR(x$, "abc") PRINT "Contains abc" DEFAULT PRINT "Value not found" END SELECT
' Nor this SELECT x$ CASE INSTR(x$, "abc") > 0 PRINT "Contains abc" DEFAULT PRINT "Value not found" END SELECT
' Nor this SELECT x$ CASE VAL(INSTR(x$, "abc")) PRINT "Contains abc" DEFAULT PRINT "Value not found" END SELECT
' Nor this SELECT x$ CASE VAL(INSTR(x$, "abc")) > 0 PRINT "Contains abc" DEFAULT PRINT "Value not found" END SELECT rod
|
|
|
Post by Pjot on Jul 22, 2011 8:39:25 GMT 1
Hi Rod,
The error dialogue is somewhat cryptic but it does explain what is going on: you are trying to compare a string with an integer number.
First, there is a "SELECT x$", meaning that you want to find out the value contained by string variable "x$". Then, the CASE statements are using INSTR or VAL, all of which are returning a number.
So this is correct usage of SELECT/CASE:
x$ = "123abc456"
SELECT x$ CASE "abc" PRINT "String is 'abc'" CASE "def" PRINT "String is 'def'" DEFAULT PRINT "String is some other value" END SELECT
...because now you compare "x$" with actual strings. However it doesn't provide the information you need.
If you want to find out if the substring "abc" is part of "x$" then simply use IF/THEN:
x$ = "123abc456" IF INSTR(x$, "abc") THEN PRINT "Contains abc"
Regards Peter
|
|
|
Post by vovchik on Jul 22, 2011 9:47:40 GMT 1
Dear Rod, What you might try is the following: x$ = "123abc456" y$ = "abc" a = INSTR(x$, y$)
SELECT a CASE 0 PRINT "Value not found" DEFAULT PRINT "Value found" END SELECT
IF a THEN PRINT "Value found" ELSE PRINT "Value not found" END IF
If you need more control, have a look at www.basic-converter.org/strcmp.bac.html, which tries to emulate the C strcmp function. With kind regards, vovchik
|
|
|
Post by GatorDog on Jul 22, 2011 14:36:36 GMT 1
Hi all,
The program is reading lines from a file and checking for several substrings. I ended up using one line IF/THEN statements. In this case it is more succinct than the SELECT/CASE. And easier to follow.
Thanks for reply rod
|
|