Issue nesting variables in csh.

The variables given are already defined ($file1-$file3, $obsid1-$obsid3, and $n=3). When I go to run the code, the terminal outputs "Missing }." I believe the error is with the nesting of variables. It would save a lot of time getting this while loop working.

set i = 1     

while (${i} <= ${n})
dmcopy "${file${i}}[energy=500:7000&&(x,y)=circle(${c},${r})][bin sky=1]" ./img/${obsid${i}}_5_70_b1.img      

@ i++     

end   

This is the snippet I believe has the "Missing }." error somewhere in it. I can't seem to find anything wrong. Does csh support nesting like this or will I need to try something else?

(Skipping the compulsary "don't use csh" reply.)
No, the assembly of a variable like ${file${i}} doesn't work.
You can use a list, for example

set files=( $file1 $file2 $file3 )
set i=1
echo "$files[$i]"

Or, as the last resort, try eval

set i=1
eval set file=\$file$i
echo "$file"

:slight_smile:

What is a good alternative that functions the way I need it to?

set files=( "$file1" "$file2" "$file3" )
set obsids=( "$obsid1" "$obsid2" "$obsid3" )

set i=1

while ($i <= $n)
  dmcopy "${files[$i]}[energy=500:7000&&(x,y)=circle(${c},${r})][bin sky=1]" "./img/${obsids[$i]}_5_70_b1.img"

  @ i++

end

The above solution worked.

1 Like