View on GitHub

IRAF Community Distribution

IRAF maintained by the community

Home | Installation | Packages | X11IRAF | PyRAF | Forum

Simple scripting question 2

Gabriel wrote on Apr 14, 2009

Hi (me again),

I'm trying to assign values read from a file to variables with different names (in a procedure script). Maybe an example will explain better what I want to do:

procedure ()

struct *list

begin

int i
string variable

list = 'file1'
i = 1
while (fscan (list, variable + i) != EOF) {
i = i + 1
}

end

My goal is to end up with: variable1 = xxx, variable2 = yyy, variable3 = zzz, etc.; where the values to store in the 'variables' are stored in the 'file1' file:

xxx
yyy
zzz
...

I hope I made myself clear.
Cheers!

Mike Fitzpatrick wrote on Apr 14, 2009

Variables can't be created dynamically as you are doing (i.e. you only declare 'variable', not 'variable1', 'variable2', etc). If you have some small number of things to read (e.g. 3-5) then the simplest thing is something like


list = "myfile"
fscan (list, variable1)
fscan (list, variable2)
    :


Another way is to use an array of values, e.g.


    string var[5]

    list = "zin"
    for (i=1; fscan(list,s1) != EOF; i=i+1)
        var[i] = s1
    
    for (i=1; i <= 5; i=i+1)
        print (var[i])
 


Note arrays are 1-indexed and you cannot scan into them directly. You'll also have to know how many strings to expect so you can declare the array with some max value.

If the values on the lines have some meaning you might be better off with the first option where you can have meaningful variable names to use in the script.

-Mike

Last post on Apr 14, 2009