| Re: Counting header lines in a file [message #94611 is a reply to message #94605] |
Mon, 24 July 2017 08:30   |
Markus Schmassmann
Messages: 129 Registered: April 2016
|
Senior Member |
|
|
On 07/24/2017 03:56 PM, thtran296@gmail.com wrote:
>> Once you read a line, you can parse it using the string functions. For example, strmid(line, 0, 1) returns the first character of the string. Then you can test if it is a letter or a number so that you can decide if it belongs to the header or to the data.
>
> The function strmid() returns a string to me, even if the input is a number.
> For example,
> a = 123456
> result = strmid(strtrim(a,2),1,4) & print, result & help, result
> IDL print:
> 2345
> STRING = '2345'
>
> For this reason, when I use the ISA() function to test if it is a string or number, it would return string to everything. So how can I test if it is a letter or a number? Is there another function besides ISA() ?
I would recommend using the STRMATCH function to test whether your line
contains numbers. Example:
tab=string(9b)
regex1='^[\ '+tab+']*[0-9]*[\ '+tab+']*[0-9]*[\ '+tab+']*$'
openr, lun, file, /get_lun
header=!null
repeat begin
point_lun, -lun, currentlocation
readf, lun, line
header=[header,line]
endrep until strmatch(line,regex1)
header=header[0:-2]
point_lun, lun, currentlocation
data = fltarr(cols,rows - n_elements(header))
.....
Often used, but very bad programming style is to abuse the error
handling system.
function isa_number, string, number=number
err_no=0
catch, err_no
if err_no ne 0 then begin
catch,/cancel
message, /reset
return, 0b
endif
number=0
reads, string, number
return, 1b
end
Both approaches need to be refined before use, (e.g. float instead of
integers), but the idea should be clear.
Good luck, Markus
|
|
|
|