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

check if the value is a numerical or a character

rshen98
初學者
1,045 檢視
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 積分
10 回應
TimP
榮譽貢獻者 III
1,045 檢視
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.
rshen98
初學者
1,045 檢視
Could you giv me an example how to do it?
TimP
榮譽貢獻者 III
1,045 檢視

character(len=96)instring,fname

open(unit=infile, file=fname)

read(infile,'(A)')instring

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

TimP
榮譽貢獻者 III
1,045 檢視

character(len=96)instring,fname

open(unit=infile, file=fname)

read(infile,'(A)')instring

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

TimP
榮譽貢獻者 III
1,045 檢視

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

Steven_L_Intel1
1,045 檢視
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.
TimP
榮譽貢獻者 III
1,045 檢視
Sorry about the single = signs, where == is required.
larsm
初學者
1,045 檢視
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
anthonyrichards
新貢獻者 III
1,045 檢視
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.
rshen98
初學者
1,045 檢視
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.
回覆