- Als neu kennzeichnen
- Lesezeichen
- Abonnieren
- Stummschalten
- RSS-Feed abonnieren
- Kennzeichnen
- Anstößigen Inhalt melden
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 Lösung
- Als neu kennzeichnen
- Lesezeichen
- Abonnieren
- Stummschalten
- RSS-Feed abonnieren
- Kennzeichnen
- Anstößigen Inhalt melden
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 kopiert
5 Antworten
- Als neu kennzeichnen
- Lesezeichen
- Abonnieren
- Stummschalten
- RSS-Feed abonnieren
- Kennzeichnen
- Anstößigen Inhalt melden
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
- Als neu kennzeichnen
- Lesezeichen
- Abonnieren
- Stummschalten
- RSS-Feed abonnieren
- Kennzeichnen
- Anstößigen Inhalt melden
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.
- Als neu kennzeichnen
- Lesezeichen
- Abonnieren
- Stummschalten
- RSS-Feed abonnieren
- Kennzeichnen
- Anstößigen Inhalt melden
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
- Als neu kennzeichnen
- Lesezeichen
- Abonnieren
- Stummschalten
- RSS-Feed abonnieren
- Kennzeichnen
- Anstößigen Inhalt melden
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
- Als neu kennzeichnen
- Lesezeichen
- Abonnieren
- Stummschalten
- RSS-Feed abonnieren
- Kennzeichnen
- Anstößigen Inhalt melden
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.
Antworten
Themen-Optionen
- RSS-Feed abonnieren
- Thema als neu kennzeichnen
- Thema als gelesen kennzeichnen
- Diesen Thema für aktuellen Benutzer floaten
- Lesezeichen
- Abonnieren
- Drucker-Anzeigeseite