- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi All,
I need to determine whether a string represents numeric data or character data. My method for this has always been something like this:
real*4 num
read(FieldValues(i,j),*,iostat=ios)num
If ios has value 0 it's numeric, otherwise it's a character string.
Unfortunately the data set I am working with at present often contains a single "/" as the data item, and this is read successfully as a number. So I have two questions: why is "/" read as a number? and what is the best way to determine whether a string represents a number or just characters?
With many thanks in advance
Mike
I need to determine whether a string represents numeric data or character data. My method for this has always been something like this:
real*4 num
read(FieldValues(i,j),*,iostat=ios)num
If ios has value 0 it's numeric, otherwise it's a character string.
Unfortunately the data set I am working with at present often contains a single "/" as the data item, and this is read successfully as a number. So I have two questions: why is "/" read as a number? and what is the best way to determine whether a string represents a number or just characters?
With many thanks in advance
Mike
Link Copied
3 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Mike,
What happens if you put a format specifier instead if * in the read statement?
Additionally, you can check for the presence of only 0...9, ., +, -, E, D any other characters would definitely mean it is a character string.
David
What happens if you put a format specifier instead if * in the read statement?
Additionally, you can check for the presence of only 0...9, ., +, -, E, D any other characters would definitely mean it is a character string.
David
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi David,
I replaced the * with an unqualified "F" - and it works! So easy, why didn't I think of it?
Many thanks
Mike
I replaced the * with an unqualified "F" - and it works! So easy, why didn't I think of it?
Many thanks
Mike
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This is a classic trap Fortran programmers fall into. List-directed formatting is usually much more liberal in what it will accept than is explicit formatting. Note that just "F" will provide a default width and number of fraction digits. If the user does not enter a decimal point, you may get unexpected results. I suggest instead 'BN,F12.0' where the 12 is whatever the width of your string is. BN makes sure that blanks are ignored (they would be by default, unless you used certain compatibility switches such as /vms.)

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