Finding a number in Text

Windows specific forum
PB&J Lover
Enthusiast
Enthusiast
Posts: 212
Joined: Fri Apr 22, 2005 2:07 pm
Location: U.S.A.
Contact:

Finding a number in Text

Post by PB&J Lover »

Hello PB users,

I'm trying to find a number in a line a text. I tried this but PB doesn't return 0 when you Valf("Text") like some BASICS do.

Code: Select all

Procedure.f GetNumber(Text$)
For index = 1 To WordCount(Text$)
   If ValF(StringField(Text$,index," ")) * 1.0
      ProcedureReturn = StrF(StringField(Text$,index," "))
   EndIf
Next index
EndProcedure
Any ideas? I want it to pick out the 3 in this sentence: "min 3 inches wood around." Something like that.

Here are the procedures for use in the code above:

Code: Select all

Procedure.s RemoveExtraSpaces(Text$)
  While FindString(Text$,"  ",1) : Text$ = ReplaceString(Text$,"  "," ", 1) : Wend
ProcedureReturn Text$
EndProcedure

Procedure.l WordCount(Text$)
count.l = 1 : Text$ = RemoveExtraSpaces(Text$)
 While StringField(Text$, count, " ") : count + 1 : Wend
ProcedureReturn count - 1
EndProcedure
-- DB

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius — and a lot of courage — to move in the opposite direction.

Albert Einstein
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

Code: Select all

Procedure.s RemoveExtraSpaces(Text$) 
  While FindString(Text$,"  ",1) : Text$ = ReplaceString(Text$,"  "," ", 1) : Wend 
ProcedureReturn Text$ 
EndProcedure 

Procedure.l WordCount(Text$) 
count.l = 1 : Text$ = RemoveExtraSpaces(Text$) 
 While StringField(Text$, count, " ") : count + 1 : Wend 
ProcedureReturn count - 1 
EndProcedure

Procedure.f GetNumber(Text$) 
For index = 1 To WordCount(Text$) 
   x.f=ValF(StringField(Text$,index," ")) * 1.0 
		If x<>0
    	ProcedureReturn x
   EndIf 
Next index 
EndProcedure 


x.f=GetNumber("min 3.7 inches wood around.")
Debug(StrF(x))
This returned 3.7

using PureBasic 3.94b3

-Anthony
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
PB&J Lover
Enthusiast
Enthusiast
Posts: 212
Joined: Fri Apr 22, 2005 2:07 pm
Location: U.S.A.
Contact:

number in text

Post by PB&J Lover »

So why didn't mine work? It is essentially the same.

Thanks.
-- DB

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius — and a lot of courage — to move in the opposite direction.

Albert Einstein
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Re: number in text

Post by Pupil »

PB&J Lover wrote:So why didn't mine work? It is essentially the same.

Thanks.
>ProcedureReturn = StrF(StringField(Text$,index," "))
You don't get a compiler error for this line? The syntax is all screwed, you must drop the equal sign! Also you must return the type that is expected from this procedure. You return a string, but you've declared the procedure to return a float.
PB&J Lover
Enthusiast
Enthusiast
Posts: 212
Joined: Fri Apr 22, 2005 2:07 pm
Location: U.S.A.
Contact:

Post by PB&J Lover »

Oops! You are right. Thanks. :D
-- DB

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius — and a lot of courage — to move in the opposite direction.

Albert Einstein
Post Reply