QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: hanness on April 10, 2021, 06:13:45 pm

Title: Could someone help me with some simple logic I'm failing to understand?
Post by: hanness on April 10, 2021, 06:13:45 pm
I'm sure that I'm missing something really simple here, but I'm a little bit confused about how I can get some Boolean results out of something like an INSTR.

Here is an example of what I am trying to do...

Suppose I have a string that I set like this:

a$ = "hello world"

Now I run this code:

If InStr(a$, "world") Then
    Print "The word world was present in the string"
End If

In that case the message will be printed because InStr evaluates to TRUE since "world" is present in the string.

Now, suppose I want to take an action if "WORLD" is in the string AND "hello" is NOT in the string.

Here is how I tried to structure this:

If (InStr(a$, "world")) AND (NOT (InStr(a$,"hello"))) Then
    Print "The word world was present and hello was not"
End If

I would expect that to fail because both "hello" and "world" are present and yet the message is still displayed.

I realize that I could perform 2 seperate operations, 1 to check for the presence of "world" and another to check for "hello" but I wanted to see if this could be one on a single line.

I realize that INSTR does not return a simple TRUE or FALSE, rather, it returns the position the string sought, which would be 0 if not present, but isn't there some simple way to convert that into a simple TRUE or FALSE?

Any thoughts on this?
Title: Re: Could someone help me with some simple logic I'm failing to understand?
Post by: hanness on April 10, 2021, 06:24:52 pm
LOL. Been looking for quite a while, 3 minutes after my post I figured it out.

My apologies, please ignore.
Title: Re: Could someone help me with some simple logic I'm failing to understand?
Post by: SMcNeill on April 10, 2021, 08:10:33 pm
If (InStr(a$, "world")) <>  0 AND (NOT (InStr(a$,"hello"))) = -1 Then....

Will the above work?
Title: Re: Could someone help me with some simple logic I'm failing to understand?
Post by: FellippeHeitor on April 10, 2021, 11:50:13 pm
If InStr(a$, "world") > 0 AND InStr(a$,"hello") = 0 Then
    Print "The word world was present and hello was not"
End If
Title: Re: Could someone help me with some simple logic I'm failing to understand?
Post by: FellippeHeitor on April 10, 2021, 11:51:07 pm
Also notice that InStr is case sensitive.
Title: Re: Could someone help me with some simple logic I'm failing to understand?
Post by: Pete on April 11, 2021, 12:58:27 am
IF INSTR(LCASE$(a$), "world") AND INSTR(LCASE$(a$), "hello") = 0 THEN
    ' do whatever
END IF

Now it's insensitive, like me.

Pete
Title: Re: Could someone help me with some simple logic I'm failing to understand?
Post by: euklides on April 11, 2021, 05:36:09 am
a$="hello world and every body"

'With a variable (here ok), you can create complex conditions.

ok=0
'here 4 tests with 'word", with "hello", without 'universe', len(a$)<200...
if instr(ucase$(a$),"HELLO")>0 then ok=ok+1
if instr(ucase$(a$),"WORD")>0 then ok=ok+1
if instr(ucase$(a$),"UNIVERSE")=0 then ok=ok+1
if len(a$)<200 then ok=ok+1

and so on...

Now'
if ok=4 then print "yes" else print" no"
sleep




Title: Re: Could someone help me with some simple logic I'm failing to understand?
Post by: FellippeHeitor on April 11, 2021, 08:05:02 am
IF INSTR(LCASE$(a$), "world") AND INSTR(LCASE$(a$), "hello") = 0 THEN
    ' do whatever
END IF

Now it's insensitive, like me.

Pete

Pete’s joke aside, @hanness notice InStr is case sensitive and will not find "World" in "Hello, world!".
Title: Re: Could someone help me with some simple logic I'm failing to understand?
Post by: FellippeHeitor on April 11, 2021, 08:08:48 am
You can make both arguments be all capitals with UCase$() if you want to make search case-insensitive:

If InStr(UCase$(a$), UCase$("world")) > 0 AND InStr(UCase$(a$), UCase$("hello")) = 0 Then
    Print "The word world was present and hello was not"
End If
Title: Re: Could someone help me with some simple logic I'm failing to understand?
Post by: SMcNeill on April 11, 2021, 08:29:23 am
You can make both arguments be all capitals with UCase$() if you want to make search case-insensitive:

If InStr(UCase$(a$), UCase$("world")) > 0 AND InStr(UCase$(a$), UCase$("hello")) = 0 Then
    Print "The word world was present and hello was not"
End If

What’s wrong with Pete’s method for insensitive comparison?  Seems to me that it’d work fine without the extra overhead of that second UCASE$ needed.

IF INSTR(LCASE$(a$), "world") <> 0 AND INSTR(LCASE$(a$), "hello") = 0 THEN
    ' do whatever
END I
Title: Re: Could someone help me with some simple logic I'm failing to understand?
Post by: FellippeHeitor on April 11, 2021, 10:35:41 am
What’s wrong with Pete’s method for insensitive comparison?  Seems to me that it’d work fine without the extra overhead of that second UCASE$ needed.

IF INSTR(LCASE$(a$), "world") <> 0 AND INSTR(LCASE$(a$), "hello") = 0 THEN
    ' do whatever
END I

Nothing wrong, I just thought it was a joke and that he was pointing out the difference between INSTR and InStr, when I failed to see the LCASE in there. My bad.
Title: Re: Could someone help me with some simple logic I'm failing to understand?
Post by: Pete on April 11, 2021, 11:15:39 pm
LOL - I parsed out laughing. :D

Pete
Title: Re: Could someone help me with some simple logic I'm failing to understand?
Post by: hanness on April 14, 2021, 02:30:45 pm
Thanks, everyone. I saw my logic error minutes after I posted the original question even though I had been at it for several hours.

You ever just keep looking at something with the same mindset and just not seeing something right there in front of you?

What's funny is that the little time break where I composed the question and posted it here, followed by a quick little break to get a drink allowed me to look at it just slightly differently when I came back and that was all I needed.
Title: Re: Could someone help me with some simple logic I'm failing to understand?
Post by: bplus on April 14, 2021, 02:39:54 pm
Yep, taking a break or explaining problem to someone else in words often helps :)

And sometimes you just keep missing something simple and sometimes you just don't know.
Title: Re: Could someone help me with some simple logic I'm failing to understand?
Post by: OldMoses on April 15, 2021, 08:57:15 pm
A good stiff drink often helps change my mindset too.