Author Topic: Could someone help me with some simple logic I'm failing to understand?  (Read 4106 times)

0 Members and 1 Guest are viewing this topic.

Offline hanness

  • Forum Regular
  • Posts: 210
    • View Profile
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?

Marked as best answer by hanness on April 10, 2021, 02:25:07 pm

Offline hanness

  • Forum Regular
  • Posts: 210
    • View Profile
LOL. Been looking for quite a while, 3 minutes after my post I figured it out.

My apologies, please ignore.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
If (InStr(a$, "world")) <>  0 AND (NOT (InStr(a$,"hello"))) = -1 Then....

Will the above work?
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

FellippeHeitor

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

FellippeHeitor

  • Guest
Also notice that InStr is case sensitive.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
IF INSTR(LCASE$(a$), "world") AND INSTR(LCASE$(a$), "hello") = 0 THEN
    ' do whatever
END IF

Now it's insensitive, like me.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline euklides

  • Forum Regular
  • Posts: 128
    • View Profile
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




« Last Edit: April 11, 2021, 05:39:24 am by euklides »
Why not yes ?

FellippeHeitor

  • Guest
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!".

FellippeHeitor

  • Guest
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

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
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
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

FellippeHeitor

  • Guest
Re: Could someone help me with some simple logic I'm failing to understand?
« Reply #10 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.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Could someone help me with some simple logic I'm failing to understand?
« Reply #11 on: April 11, 2021, 11:15:39 pm »
LOL - I parsed out laughing. :D

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline hanness

  • Forum Regular
  • Posts: 210
    • View Profile
Re: Could someone help me with some simple logic I'm failing to understand?
« Reply #12 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.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Could someone help me with some simple logic I'm failing to understand?
« Reply #13 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.

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: Could someone help me with some simple logic I'm failing to understand?
« Reply #14 on: April 15, 2021, 08:57:15 pm »
A good stiff drink often helps change my mindset too.