Page 1 of 1

Finding a number in Text

Posted: Mon Jul 25, 2005 5:12 pm
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

Posted: Mon Jul 25, 2005 6:05 pm
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

number in text

Posted: Mon Jul 25, 2005 6:10 pm
by PB&J Lover
So why didn't mine work? It is essentially the same.

Thanks.

Re: number in text

Posted: Mon Jul 25, 2005 7:43 pm
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.

Posted: Mon Jul 25, 2005 7:58 pm
by PB&J Lover
Oops! You are right. Thanks. :D