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

check if the value is a numerical or a character

rshen98
Beginner
1,030 Views
I am new in this board and a new user. Here is my naive question:
I have some data file which are mixed with numerical values and characters. I want only numerical values. How can I check if a value is numerical or charcter? My data is looked like
HISTORIC PRECIPITATION [IN] STATION: 897
Annual Data Display: Summary for period ending 1929/ 9/30
Day Oct Nov Dec Jan Feb Mar Apr May Jun Jul Aug Sep
1 1.980 0.000 0.231 0.605 0.682 0.242 0.000 0.000 0.814 0.000 0.000 0.110
2 1.089 0.000 0.000 0.880 0.220 0.605 0.000 1.012 0.000 0.044 0.000 0.011
3 0.165 0.044 0.000 0.000 0.000 0.088 0.044 0.088 0.000 0.000 0.088 0.000
4 0.880 0.231 0.000 0.000 0.000 0.000 0.275 0.110 0.000 0.000 0.000 0.000
5 0.869 0.132 0.000 0.000 0.000 0.935 0.000 0.220 0.000 0.000 0.000 0.000
0 Kudos
10 Replies
TimP
Honored Contributor III
1,030 Views
You could read each line as a character string. If you know what the acceptable formats are, and can distinguish the strings containing numerical data, you can use internal read to process the strings containing numeric fields after identifying them.
0 Kudos
rshen98
Beginner
1,030 Views
Could you giv me an example how to do it?
0 Kudos
TimP
Honored Contributor III
1,030 Views

character(len=96)instring,fname

open(unit=infile, file=fname)

read(infile,'(A)')instring

if(instring(1:22)='HISTORIC PRECIPITATION')then

0 Kudos
TimP
Honored Contributor III
1,030 Views

character(len=96)instring,fname

open(unit=infile, file=fname)

read(infile,'(A)')instring

if(instring(1:22)='HISTORIC PRECIPITATION')then

0 Kudos
TimP
Honored Contributor III
1,030 Views

Message Edited by tim18 on 08-25-2004 03:58 PM

0 Kudos
Steven_L_Intel1
Employee
1,030 Views
Whatever you do, don't do a read with format * (list-directed) in an attempt to "validate" the input. You will find it accepting far more than you want.
0 Kudos
TimP
Honored Contributor III
1,030 Views
Sorry about the single = signs, where == is required.
0 Kudos
larsm
Beginner
1,030 Views
Here is a solution that I extracted from my software. The basic procedures do some more checking (to get away from some of the things that list-directed format passes, as Steve warned about) and are more general, but this solution works well, especially if you can assert that the lines you are interested in processing have the same number of numbers in them and that they occur in the beginning of the line. I have found that this is often the case.

I attach the function, "get_dirtfile", and a driver for your example, that I have put in the "example.txt" file.

Best wishes

Lars
0 Kudos
anthonyrichards
New Contributor III
1,030 Views
Why bother?
Why not paste the text into Excel and use Data menu to convert text to columns? Then you can pick out the numbers you want.
0 Kudos
rshen98
Beginner
1,030 Views
Thank all of you for the help. I finally solve the problem. BTW, Excel doesn't help in this case because the file is large,has more rows taht excel can handle.
0 Kudos
Reply