Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29010 Discussions

determining if string is valid numerical type

tracyx
New Contributor I
810 Views
I have a string that I want to convert into a numerical type (integer or real). Is there an elegant way to determine whether the string contains a valid numerical type, or is it necessary to loop over all the characters to determine this?

Thanks, Tracy
0 Kudos
1 Solution
Steven_L_Intel1
Employee
810 Views
Quoting - anthonyrichards

Use internal READ and use ERR= branching until you get no error. e.g.

READ(STRING,ERR=9999, *) realvariable
READ(STRING,ERR=9999, *) integervariable

Perhaps you meant READ(STRING,*,ERR=9999)? In any event, I do not recommend using list-directed reads for doing input validation. It is much more liberal about what it will consider valid input than is wanted here. For example, it will accept a comma or a slash. I would suggest an explicit format with an In or Gn.0 format as needed, where n is the width of the string.

View solution in original post

0 Kudos
5 Replies
anthonyrichards
New Contributor III
810 Views
Quoting - tracyx
I have a string that I want to convert into a numerical type (integer or real). Is there an elegant way to determine whether the string contains a valid numerical type, or is it necessary to loop over all the characters to determine this?

Thanks, Tracy

Use internal READ and use ERR= branching until you get no error. e.g.

READ(STRING,ERR=9999, *) realvariable
READ(STRING,ERR=9999, *) integervariable
0 Kudos
Steven_L_Intel1
Employee
811 Views
Quoting - anthonyrichards

Use internal READ and use ERR= branching until you get no error. e.g.

READ(STRING,ERR=9999, *) realvariable
READ(STRING,ERR=9999, *) integervariable

Perhaps you meant READ(STRING,*,ERR=9999)? In any event, I do not recommend using list-directed reads for doing input validation. It is much more liberal about what it will consider valid input than is wanted here. For example, it will accept a comma or a slash. I would suggest an explicit format with an In or Gn.0 format as needed, where n is the width of the string.
0 Kudos
tracyx
New Contributor I
810 Views

Perhaps you meant READ(STRING,*,ERR=9999)? In any event, I do not recommend using list-directed reads for doing input validation. It is much more liberal about what it will consider valid input than is wanted here. For example, it will accept a comma or a slash. I would suggest an explicit format with an In or Gn.0 format as needed, where n is the width of the string.

Thank you both, that is perfect for my needs.

Kind regards, Tracy
0 Kudos
jimdempseyatthecove
Honored Contributor III
810 Views

Tracy,

In your debug build, consider not only using the Gn.0 or other format specifier, but also include your own sanity check for spurious input. Then throw everything imagineable at the routine for testing. A false positive is not an indication that the data is correct. And, remember actual input data may be stranger than you can imagine.

Jim Dempsey
0 Kudos
rase
New Contributor I
810 Views
John Burkardt maintains various libraries with Fortran source code, among them one called CHRPAK with routines for conversion of character strings and other string-related functions (http://people.sc.fsu.edu/~burkardt/f_src/chrpak/chrpak.html) . Maybe you can find there what you are looking for.
0 Kudos
Reply