;+ ; NAME: ; nthline ; ; PURPOSE: (one line) ; Return the nth line in a file ; ; DESCRIPTION: ; Return the nth line in a file ; ; CATEGORY: ; File IO ; ; CALLING SEQUENCE: ; line = nthline(filename, linenum) ; ; INPUTS: ; filename - file from which to get lines ; linenum - the line number to return (long or array of long) ; ; OPTIONAL INPUT PARAMETERS: ; None ; ; KEYWORD INPUT PARAMETERS: ; /VERBOSE -- If set, print diagnostics to screen. ; ; KEYWORD OUTPUT PARAMETERS: ; NSTARS -- Returns the number of successfully-retrieved lines. ; ; OUTPUTS: ; If input is a scalar, then output is a single string. ; If input is a vector, then output is an array of strings. ; If a line is not found (linenum out of range), returned value is ''. ; ; COMMON BLOCKS: ; None ; ; SIDE EFFECTS: ; None ; ; PROCEDURES: ; repeated calls to readf ; ; RESTRICTIONS: ; ; EXAMPLE: ; print, nthline(102,[430L, 1000L]) ; ; MODIFICATION HISTORY: ; Written 2006 Apr 23 Leslie Young SwRI ;- function nthline, filename, linenum openr, lun, filename, /get_lun nline = n_elements(linenum) lines = strarr(nline) s = sort(linenum) n = -1L ; number of lines read in the file i = 0L cont = 1 while i lt nline and cont do begin nextnum = linenum[s[i]] ; print, i, nextnum tmp = ' ' while not eof(lun) and n lt nextnum do begin n = n + 1 readf,lun,tmp endwhile if n eq nextnum then begin lines[i] = tmp i = i + 1 endif else begin cont = 0 endelse end close, lun & free_lun,lun return, lines end