while loop usage and fscan
Frank Valdes wrote on Mar 23, 2000
Q: I have a question concerning the while loop in a cl script:
I would like to read a file line by line and save each line as a
different variable. How many lines the file contains and how many
variables are needed (and the names of them) depends on a query
parameter.
therefore i need a loop with a counter, like the 'for' loop.
but as far as i understood cl, it is not possible to use the 'fscan'
without a while (!= EOF) loop.
--> does this while loop return a counter? how can i refere to it?
--> 'nscan returns a number' is written in the help page. can i use
nscan for reading a file? how can i refere to this number? where can i
find a example for the use of nscan?
A: The fscan does not have to be used in a while. It returns an integer
giving the number of elements read or EOF. So you can do something like
for (i=1; i<=maxlines; i=i+1) {
if (fscan (list, x, y, z) == EOF)
break
if (nscan() != 3)
next
...
}
You can use the fscan return value without having to use nscan:
for (i=1; i<=maxlines; i=i+1) {
j = fscan (list, x, y, z)
if (j == EOF)
break
if (j != 3)
next
...
}
The CL language can do some very useful things but it takes time and experience
to learn. Also knowing other languages like C helps. The only programming
documentation is
ftp://iraf.noao.edu/iraf/docs/script.ps.Z
You can do "? language" to get a menu of commands in the language package
which has things you might often use in scripts. There are help pages
for many of these though you sometimes need to quote them to avoid them
being interpreted as commands:
cl> help "strings"
cl> help "for"
cl> help "fscan"
Last post on Mar 23, 2000