QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: MLambert on November 07, 2019, 06:56:07 am

Title: Embedded numerics in a text field
Post by: MLambert on November 07, 2019, 06:56:07 am
Hi,

I am looking for an easy way to check if a text field has numerics in it ..

Thanks,

Mike
Title: Re: Embedded numerics in a text field
Post by: SMcNeill on November 07, 2019, 07:07:00 am
INPUT text$
search$ = “0123456789”
FOR I = 1 TO 10
   IF INSTR(text$, MID$(search$, I, 1)) THEN EXIT FOR
NEXT
IF I < 11 THEN PRINT “There’s numbers in there!” ELSE PRINT “Nope.  No numbers here.”
Title: Re: Embedded numerics in a text field
Post by: Qwerkey on November 07, 2019, 07:17:00 am
Yep.  That's how to look for a number.  Actually, there's a missing right bracket: IF INSTR(text$, MID$(search$, I, 1)) THEN EXIT FOR (Steve has now modified)

And the anti- EXIT FOR brigade will be up-in-arms.

Title: Re: Embedded numerics in a text field
Post by: FellippeHeitor on November 07, 2019, 07:23:11 am
And the anti- EXIT FOR brigade will be up-in-arms.

Those shall not prosper in a BASIC forum.
Title: Re: Embedded numerics in a text field
Post by: SMcNeill on November 07, 2019, 08:49:43 am
Yep.  That's how to look for a number.  Actually, there's a missing right bracket: IF INSTR(text$, MID$(search$, I, 1)) THEN EXIT FOR

And the anti- EXIT FOR brigade will be up-in-arms.

For them, I present:

10 INPUT text$
20 search$ = “0123456789”
30 FOR I = 1 TO 10
40 IF INSTR(text$, MID$(search$, I, 1)) GOTO 60
50 NEXT
60 IF I < 11 THEN PRINT “There’s numbers in there!” ELSE PRINT “Nope.  No numbers here.”

No code blocks, indenting, or anything else for those anti-EXIT guys!
Title: Re: Embedded numerics in a text field
Post by: Petr on November 07, 2019, 09:57:58 am
And my solution...

Code: [Select]
INPUT "Insert something..."; s$
FOR a = 1 TO LEN(s$)
    IF ASC(s$, a) >= 48 AND ASC(s$, a) <= 57 THEN nr = 1: EXIT FOR ELSE nr = 0
NEXT
IF nr THEN PRINT "Number found." ELSE PRINT "Number not found"
Title: Re: Embedded numerics in a text field
Post by: bplus on November 07, 2019, 12:00:27 pm
If I were MLambert, I would be feeling unsatisfied with above solutions, for instance:
What if one wants to extract the real numbers in say "You can reach me at -22.17 degrees latitude, -44.96 degrees longitude."

FOR anti-EXITers,
There is also the old short circuit method of exiting a FOR by setting the index past the top limit but then the value of index has to be modified if you want to use it after exit.
Title: Re: Embedded numerics in a text field
Post by: SMcNeill on November 07, 2019, 12:08:52 pm
If I were MLambert, I would be feeling unsatisfied with above solutions, for instance:
What if one wants to extract the real numbers in say "You can reach me at -22.17 degrees latitude, -44.96 degrees longitude."

But that wasn’t the question presented.  All he wanted was to check for numeric values, not extract them.  😉
Title: Re: Embedded numerics in a text field
Post by: bplus on November 07, 2019, 12:15:53 pm
But that wasn’t the question presented.  All he wanted was to check for numeric values, not extract them.  😉

OK another great reading job by bplus! ;D

I guess I wanted to make the problem more challenging.
Title: Re: Embedded numerics in a text field
Post by: Qwerkey on November 08, 2019, 04:48:09 am
For a simple Yes/No check, I would generally use a WHILE/WEND with a Truth check, and leave the EXIT FOR routine for when things get more complex and you need to end the loop prematurely.
Code: QB64: [Select]
  1. CONST False = 0
  2. INPUT Text$
  3. Search$ = "0123456789"
  4. ThisRatherLovelyTextContainsNumbers%% = False
  5. CharPos%% = 1
  6. WHILE CharPos%% <= 10 AND NOT ThisRatherLovelyTextContainsNumbers%%
  7.     IF INSTR(Text$, MID$(Search$, CharPos%%, 1)) THEN ThisRatherLovelyTextContainsNumbers%% = NOT False
  8.     CharPos%% = CharPos%% + 1
  9. IF NOT ThisRatherLovelyTextContainsNumbers%% THEN
  10.     PRINT "Sorry, mate. You ain't got any numbers there.  What can I say?"
  11.     PRINT "By Jove, Sir!  I'm delighted to say that you have some numbers there."
Title: Re: Embedded numerics in a text field
Post by: MLambert on November 14, 2019, 03:56:16 am
Thank you all for the help.

I was looking maybe for a more 'clever' way,  say overlaying a bit mask and using bit patterns, without doing a loop.

Maybe a Xor Xand . I am reading 100,000,000 records.

Thanks again,

Mike
Title: Re: Embedded numerics in a text field
Post by: Jack002 on November 14, 2019, 12:14:38 pm
Question, who are these "anti- EXIT FOR brigade" and why are they up in arms?
I think a EXIT FOR is just fine, even as good as a WHILE/WEND.

Back in the C-64 days (now I imagine some pitchforks coming out) we were told, don't exit a for loop early, you leave things on the stack, do it too much and the stack will explode and you will get a mem error.

Surely this is no longer a problem? Our new and modern QB64 won't do that, right?

Maybe this brigade is up in arms over some other issue? *shrug*


(By the way, there's another way to do this
Set a var to 0
iterate thru as before, but add the INSTR to it every time
Test if its > 0 at the end.
)




Title: Re: Embedded numerics in a text field
Post by: bplus on November 14, 2019, 02:02:32 pm
Question, who are these "anti- EXIT FOR brigade" and why are they up in arms?
I think a EXIT FOR is just fine, even as good as a WHILE/WEND.

Back in the C-64 days (now I imagine some pitchforks coming out) we were told, don't exit a for loop early, you leave things on the stack, do it too much and the stack will explode and you will get a mem error.

Surely this is no longer a problem? Our new and modern QB64 won't do that, right?

Maybe this brigade is up in arms over some other issue? *shrug*


(By the way, there's another way to do this
Set a var to 0
iterate thru as before, but add the INSTR to it every time
Test if its > 0 at the end.
)

Hi Jack002,

Who? Qwerkey, maybe carryover from the Britash thing :D and/or was it Cobalt?
Found conversation: https://www.qb64.org/forum/index.php?topic=1748.msg109927#msg109927
I don't think it's just FOR loops they are avoiding with EXIT. Qwerkey, I think is sitting on the fence thinking something must be dirty about EXIT, after all it's only a four letter word :D

What are you talking about inside that: " (By the way,...   at the end )"  ?
I am connoisseur of different ways to do things.
Oh wait... I thought you were talking about EXITs still :P
Title: Re: Embedded numerics in a text field
Post by: bplus on November 14, 2019, 04:52:40 pm
No iteration = no EXIT debate, mod of Jack's idea:

Code: QB64: [Select]
  1. x$ = "99 beers on the wall."
  2. nFlag = INSTR(x$, "0") + INSTR(x$, "1") + INSTR(x$, "2") + INSTR(x$, "3") + INSTR(x$, "4") + INSTR(x$, "5") + INSTR(x$, "6") + INSTR(x$, "7") + INSTR(x$, "8") + INSTR(x$, "9") > 0
  3. IF nFlag THEN PRINT "Found numbers in '"; x$; "'" ELSE PRINT "Did not find numbers in '"; x$; "'"
  4.  
Title: Re: Embedded numerics in a text field
Post by: Jack002 on November 14, 2019, 06:18:23 pm
Very nice. I was going to set a string to "0123456789" and a for loop and MID$, but this is good too.
Wow, as many ways to do it as snowflakes!

As for the EXIT fear. Now I see.
I have on occasion set a flag in a for loop and just let it run, it will keep going after its found what it needs.
Slower, but with no EXIT .001% more elegant I suppose.
As for a while/wend, you can decide when to get out and any good programmer should be looking for these outs. I expect them. I use them. *shrug*

I've seen nasty looking code and one with lots of exits is not among them IMHO

[skip this part if you don't care about c64 basic]
The thread link you posted asked "is it bad practice to use exit for?" and the answer "no" was posted.
I recall something about jumping out of for loops willy nilly in c64 basic. We didn't have the exit for, we just had goto.
So you can use it, fine, but you were leaving the for loop info on the stack! Not good! I don't know how you fix that, but at least for this case jumping out like that was bad. *shrug*
Title: Re: Embedded numerics in a text field
Post by: Jack002 on November 16, 2019, 01:45:46 pm
[Topic creep warning!]
Upon further investigation, on the topic of not jumping out of for loops on a C64 (if you care)

I found we didn't/dont have the EXIT FOR command in c64 basic, but you can test with IF/GOTO within one, and doing that will cause problems.
A FOR loop sets up for returning back until the loop status is cleared. The stack has the address of the FOR statement and it's variable name and value. If you were to just GOTO out of it all that remains on the stack, and if done enough times your stack becomes full

I imagine the EXIT FOR command we have in QB64 will clear that and not be a problem. The issue the never EXIT people have is the (seeming) lack of elegance to see the process of the code. This seems a matter of opinion to me.