if string is a number - how to findout?

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
rblack
Pro Scripter
Posts: 87
Joined: Sat Dec 22, 2007 12:39 pm

if string is a number - how to findout?

Post by rblack » Thu Jun 05, 2008 6:59 pm

hello,

lets say i have the following code

readln>file.txt,1,answer

How can i find if the %answer% is a number not a string ? Is there some cool VB function for this?

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Thu Jun 05, 2008 7:24 pm

See this thread. You probably want the last post by Marcus.

rblack
Pro Scripter
Posts: 87
Joined: Sat Dec 22, 2007 12:39 pm

Post by rblack » Thu Jun 05, 2008 7:45 pm

propably yes, but if I use the following code:

Code: Select all

VBSTART
Function IsNumber(var)
  IsNumber = (LCase(var) = UCase(var)) and isNumeric(var)
End Function
VBEND

Readln>c:\test.txt,1,text

VBEval>IsNumber(%text%),IsNum
MessageModal>IsNum
If>IsNum=True
  MessageModal>Its a number!
Endif
And run the script, and the first line of test.txt is sth like: 345334a
I get
compile error:1006, line2 column 16, no ")" sign

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Thu Jun 05, 2008 9:10 pm

Here is a rewrite of my script from the previously referenced thread. See if you can make it fail.

Code: Select all

Let>Text=345334a 

Let>IsIntegerFlag=0

SRT>Test
  If>%var_count%>1
	Let>IsIntegerFlag=1
  EndIF
END>Test

Separate>%text%,-,var
 GoSub>Test
Separate>%text%,.,var
 GoSub>Test
Separate>%text%,%SPACE%,var
 GoSub>Test
Separate>%text%,E,var
 GoSub>Test
Separate>%text%,e,var
 GoSub>Test
Let>test_res=%text%/1
Separate>%test_res%,/,var
 GoSub>Test

If>IsIntegerFlag=0
  MDL>"%Text%" is an integer
Else
  MDL>"%Text%" is NOT an integer
EndIf


rblack
Pro Scripter
Posts: 87
Joined: Sat Dec 22, 2007 12:39 pm

Post by rblack » Thu Jun 05, 2008 9:57 pm

wow, your code works perfect.
Thats impressive, fast and correct answer.
Thank you!

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Thu Jun 05, 2008 10:13 pm

Low tech solution :lol:

Image

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Thu Jun 05, 2008 10:30 pm

Good one!!! :)

Here's roughly the same technique with a test for a dot which will slip through your original that seems to accurately test for "numbers" but needs this extra step to test for "integers".

Code: Select all

Let>text=12345
Separate>text,.,var
If>var_count>1
  Let>text3=Nope
Else
  Let>text2=text+1
  Let>text3=text2-1
EndIf
If>text<>text3
  MDL>"%text%" is not an integer
Else
  MDL>"%text%" is an integer
Endif

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Thu Jun 05, 2008 11:46 pm

Ah, but is 1.0 an integer ? :lol:

rblack
Pro Scripter
Posts: 87
Joined: Sat Dec 22, 2007 12:39 pm

Post by rblack » Sat Jun 07, 2008 9:26 am

Me_again: very nice solution :D

rblack
Pro Scripter
Posts: 87
Joined: Sat Dec 22, 2007 12:39 pm

Post by rblack » Thu Aug 21, 2008 12:17 pm

hi there again, now i need sth more complicated. I need to know if a number can be divided by 20, so i need to know if it can be a pure integer. But unfortunately none of your scripts above do that

Code: Select all

Let>text=%k%/20
MessageModal>%text%

let>k=k+1
Separate>text,.,var
If>var_count>1
  Let>text3=Nope
Else
  Let>text2=text+1
  Let>text3=text2-1
EndIf
If>text<text3>"%text%" is not an integer
  if>k>20,end
  goto>generuj
Else
  MDL>"%text%" is an integer
  if>k>20,end
Endif

label>end
the if>k>20,end is for security for debuging, because its hard to stop the script.

So i tried many combinations:
Separate>text,.,var, Separate>text,,,var
If>var_count>1, If>var_count>0

But the results are incorrect. For example it tels that 0,05 is an integer, or that 1 is not an integer, or 2 is not an integer. Arent there any build-in functions to chcec if number is an integer?

rblack
Pro Scripter
Posts: 87
Joined: Sat Dec 22, 2007 12:39 pm

Post by rblack » Thu Aug 21, 2008 12:53 pm

ok, here is what i needed:

Code: Select all

let>m={%k% MOD 20}
if>m<>0,start

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts