- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Thanks, Tracy
1 Solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Link Copied
5 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - Steve Lionel (Intel)
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page