View on GitHub

IRAF Community Distribution

IRAF maintained by the community

Home | Installation | Packages | X11IRAF | PyRAF | Forum

Creating image names within for loop and pipe-ing them

smp wrote on Oct 23, 2008

Hi,

In the following segment of procedure script, I am trying to create image names and pipe them to a string variable, one bye one.

But I am getting the error:
Attempt to access undefined local variable `str'


      :
begin
   string str
      :
   for(n=1;n<=5;n=n+1)
       {
          printf("left_%d.fits", n) | str
          print(str)
       }
     :
end


So if I define 'str' , as follows:


     :
begin
   string str
   str = "iraf"
      :
   for(n=1;n<=5;n=n+1)
       {
          printf("left_%d.fits", n) | str
          print(str)
       }
     :


I do not get error. But now the output is not what I wanted:

left_1.fits
iraf
left_2.fits
iraf
left_3.fits
iraf
left_4.fits
iraf
left_5.fits
iraf

Instead I want following output:
left_1.fits
left_2.fits
left_3.fits
left_4.fits
left_5.fits

Please help me.
Thanks

Jason Quinn wrote on Oct 23, 2008

You'll want a newline ("\n") to end the printf and don't just pipe the output of printf to the variable but use a scan call.


      :
begin
   string str
   int n
   for(n=1;n<=5;n+=1)
       {
          printf("left_%d.fits\n", n) | scan(str)
          print(str)
       }
     :
end


But the way I would do it would be like this:


     :
begin
   string str
   int n
      :
   for(n=1;n<=5;n+=1)
       {
          str="left_"//str(n)//".fits"
          print(str)
       }
     :


I also hate any n=n+1 type constructs and changed it to n+=1. I prefer even more n++ but IRAF doesn't have an increment operator.

Jason

smp wrote on Oct 23, 2008

Hi,

Thanks.

Both the ways work well here. Second way is a cute one.
About the second way:
There are 4 double quotes.
I suppose that 1st and last are for first str i.e. 'str =' . Am I right?
Also 2nd double quote is followed by concatenation , while after str(n), concatenation is followed by double quote. I have not understood this sequence clearly. Will you please explain it?

Regards
Swapnil

Mike Fitzpatrick wrote on Oct 23, 2008

The '//' is the string concatenation operator in the CL, so the way to read the line is that the variable 'str' is the concatenation of the string "left_" with the string version of the variable 'n' with the string ".fits"

-Mike

smp wrote on Oct 23, 2008

Hi,

Thanks.

Swapnil

Last post on Oct 23, 2008