Author Topic: Embedded numerics in a text field  (Read 4749 times)

0 Members and 1 Guest are viewing this topic.

Offline MLambert

  • Forum Regular
  • Posts: 115
    • View Profile
Embedded numerics in a text field
« 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

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Embedded numerics in a text field
« Reply #1 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.”
« Last Edit: November 07, 2019, 08:50:25 am by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: Embedded numerics in a text field
« Reply #2 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.

« Last Edit: November 08, 2019, 04:46:35 am by Qwerkey »

FellippeHeitor

  • Guest
Re: Embedded numerics in a text field
« Reply #3 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.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Embedded numerics in a text field
« Reply #4 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!
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Embedded numerics in a text field
« Reply #5 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"

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Embedded numerics in a text field
« Reply #6 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.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Embedded numerics in a text field
« Reply #7 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.  😉
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Embedded numerics in a text field
« Reply #8 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.

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: Embedded numerics in a text field
« Reply #9 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."

Offline MLambert

  • Forum Regular
  • Posts: 115
    • View Profile
Re: Embedded numerics in a text field
« Reply #10 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

Offline Jack002

  • Forum Regular
  • Posts: 123
  • Boss, l wanna talk about arrays
    • View Profile
Re: Embedded numerics in a text field
« Reply #11 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.
)




QB64 is the best!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Embedded numerics in a text field
« Reply #12 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
« Last Edit: November 14, 2019, 02:29:30 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Embedded numerics in a text field
« Reply #13 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.  
« Last Edit: November 14, 2019, 04:55:24 pm by bplus »

Offline Jack002

  • Forum Regular
  • Posts: 123
  • Boss, l wanna talk about arrays
    • View Profile
Re: Embedded numerics in a text field
« Reply #14 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*
« Last Edit: November 14, 2019, 06:29:36 pm by Jack002 »
QB64 is the best!