View on GitHub

IRAF Community Distribution

IRAF maintained by the community

Home | Installation | Packages | X11IRAF | PyRAF | Forum

Arrays in CL

Eugene A. Semenko wrote on Apr 10, 2007

Hello!
I would like to read list of files from @file to some array. But I don't know exactly number of records in @file.
That is I need something like

string sarray[]
int ii = 0
list = 'somefile'
while (fscan(list, s1) != EOF) {
    sarray[ii] = s1
    ii = ii + 1
}

where size of sarray increasing dynamically. How can I do this?

And the second question. Is there any method to know nomber of elements in array or image? I need something like a n_elements in IDL or $#array in PERL.

Thanks.

Mike Fitzpatrick wrote on Apr 10, 2007

Sorry, variable/dynamic arrays aren't permitted. I could easily adda new builtin function to return the declared size and dimensionality of an array but presumably any script using that array should probably already know that.

-Mike

Eugene A. Semenko wrote on Apr 10, 2007

Well, but how can I create array in the case if exact number of elements is unknown. Actually I have @file where number of records is unknown. I can of cource make a loop with counter and obtain N_elements in this way. I can make something like a $cat "@file" | wc -l, but I don't know how initiate a variable with value obtained from bash environment. I thank that more convenient method exist.

Mike Fitzpatrick wrote on Apr 10, 2007

The COUNT task can tell you how many lines are in a file, but you still can't use this within a script to declare an array based on the value (i.e. that's what I meant by 'variable sized' arrays. The best you can do is declare an array to be some number larger than you expect to use, and then perhaps use COUNT to make sure you won't overflow it. Optimally though you'd write your script so that you wouldn't need to store the file as an array internally in the first place.

-Mike

Last post on Apr 10, 2007