Operating System - OpenVMS
1753829 Members
8983 Online
108806 Solutions
New Discussion юеВ

Re: Checking a String symbol for numerics

 
SOLVED
Go to solution
Stephen Daddona
Frequent Advisor

Checking a String symbol for numerics

Have any of you folks had the occasion to check a symbol of type STRING for all numerics? I need to do that. Using F$INTEGER returns a value of zero, but zero is a valid value in the data that I'm checking.


thanks in advance
4 REPLIES 4
Steven Schweda
Honored Contributor

Re: Checking a String symbol for numerics

How close does F$TYPE() come?

HELP LEXICALS F$TYPE

alp $ x = 123
alp $ wso f$type( x)
INTEGER

alp $ x = "123"
alp $ wso f$type( x)
INTEGER

alp $ x = "1o3"
alp $ wso f$type( x)
STRING
Karl Rohwedder
Honored Contributor

Re: Checking a String symbol for numerics

You may also specify the radix, e.g.
$ A="ABC"
$ Write Sys$output F$Type(A)
STRING
$ A="%XABC"
$ Write SyS$Output F$Type(A)
INTEGER

regards kALLE
Duncan Morris
Honored Contributor
Solution

Re: Checking a String symbol for numerics

Craig,

since you specify that you are starting with a STRING symbol, you can just examine each character in turn and check for a numeric. With a bit of work, you can also allow for negative numbers, leading/trailing spaces etc.

Here is a simplistic sample

$check_for_numeric: SUBROUTINE
$!
$! Check a supplied string for all numeric
$! sets $STATUS on exit
$! 1 = numeric
$! 2 = false
$! usage CALL CHECK_FOR_NUMERIC string_to_test
$ inp_string = p1
$ len_inp = f$length(inp_string)
$ inp_sub = 0
$char_loop:
$ test_char = f$extract(inp_sub,1,inp_string)
$ if test_char .lts. "0" -
.or. test_char .gts. "9"
$ then
$ exit 2
$ endif
$ inp_sub = inp_sub + 1
$ if inp_sub .lt. len_inp
$ then
$ goto char_loop
$ endif
$ exit 1
$ENDSUBROUTINE

Duncan
Stephen Daddona
Frequent Advisor

Re: Checking a String symbol for numerics

Thanks Duncan. I was going to write something similar but I was hoping for a lexical, etc. that would do what I wanted.

Thanks, all, for the responses.