Re: Skipping Commented Line in File [message #67593 is a reply to message #67592] |
Thu, 06 August 2009 15:08  |
Paul Van Delst[1]
Messages: 1157 Registered: April 2002
|
Senior Member |
|
|
Vikram wrote:
> Hello,
>
> I was wondering if there is a way to skip a commented line in a .txt
> file, for example:
>
> #Count Time
> 1 60
> 2 80
> 3 100
>
> I would like to start from the second row data and skip the labels.
>
> Thanks,
> Vikram
I can think of several ways, but below is what I use for files that may also include
embedded blank lines and comments:
FUNCTION Is_A_CommentLine, Line
RETURN, STRMID(STRTRIM(Line,1),0,1) EQ '#'
END ; FUNCTION Is_A_CommentLine
FUNCTION Is_A_BlankLine, Line
RETURN, STRLEN(STRTRIM(Line,2)) EQ 0
END ; FUNCTION Is_A_BlankLine
PRO test
Line = ''
OPENR, fid, 'test.txt', /GET_LUN
WHILE ( NOT EOF(fid) ) DO BEGIN
; ...Read a line from the file
READF, fid, Line
; ...Cycle loop if this is an embedded comment or blank line
IF ( Is_A_CommentLine(Line) OR Is_A_BlankLine(Line) ) THEN CONTINUE
; Otherwise read the line into variables
READS, Line, x, y
PRINT, x, y
; Store x and y into array
; .... etc etc ....
ENDWHILE
FREE_LUN, fid
END
Here's my test.txt file:
#<----start of file----->
#Count Time
1 60
2 80
3 100
4 80
5 100
6 100
#embedded comment!
7 80
8 100
9 100
10 80
# comment surrounded by blank lines!
11 100
12 100
13 80
14 100
15 100
16 80
17 100
#<----end of file----->
and here the above test.pro output:
IDL> .run test
% Compiled module: IS_A_COMMENTLINE.
% Compiled module: IS_A_BLANKLINE.
% Compiled module: TEST.
IDL> test
1.00000 60.0000
2.00000 80.0000
3.00000 100.000
4.00000 80.0000
5.00000 100.000
6.00000 100.000
7.00000 80.0000
8.00000 100.000
9.00000 100.000
10.0000 80.0000
11.0000 100.000
12.0000 100.000
13.0000 80.0000
14.0000 100.000
15.0000 100.000
16.0000 80.0000
17.0000 100.000
cheers,
paulv
|
|
|