If you need a loop for which you don't know in advance how many iterations there will be, you can use the `while' statement. It works like this:
while condition do statement
Again, if you are using this in a program you can replace statement with a block of statements - the block must end with an endwhile . You can construct condition in the same way as for an if statement.
Here is an example (modtran_while.pro) It is the modtran plotting program again, but this time we don't need to know the number of lines in advance. The program reads a line at a time, stopping when it reaches the end of the file. We put the data into an array which starts off with more lines than we are likely to need. (Don't make the array big enough for ten million lines, though - you'll fill up the computer's memory!) Once we reach the end of the file, we truncate the data array to the length of the file.
;Set up array with far too many rows to contain the data modtrandata=fltarr(14,5000) openr,1,'/nfs/env-fs-04_u35/jmarsham/public_html/Teaching/IDL_course/pldat.dat' inputline = fltarr(14) ; set up a variable to hold one row of the file linecount=0 ; variable to count lines ;;;; start while loop to read in the data while not eof(1) do begin ; eof becomes true when we reach the End Of ; the File. readf,1,inputline modtrandata(*,linecount)=inputline linecount=linecount+1 endwhile modtrandata=modtrandata(*,0:linecount-1) ; truncate array to actual length ; of file. close,1OR (modtran_while2.pro)
openr,1,'/nfs/env-fs-04_u35/jmarsham/public_html/Teaching/IDL_course/pldat.dat' inputline = fltarr(14) ; set up a variable to hold one row of the file linecount=0 ; variable to count lines ;;;; start while loop to read in the data while not eof(1) do begin ; eof becomes true when we reach the End Of ; the File. readf,1,inputline if linecount eq 0 then modtrandata=inputline else modtrandata=[[modtrandata],[inputline]] linecount=linecount+1 endwhile modtrandata=modtrandata(*,0:linecount-1) ; truncate array to actual length ; of file. close,1