작성일:
 
2010. 5. 7. 15:33
 



프로그래밍에 필요한 블록문 습득
컴파일 및 실행


ex)


print, a   <- Procedure (프로시져)
index=where(a eg 0)   <- function (함수)


(twotimes.pro)
pro twotime, value
value=2.0*value
print, 'it was multiplied by 2.0'
print, value
end

컴파일 후 결과


twotime, 12 넣으면 (IDL창에)
24가 출력됨

///////////////////////////////////
pro 파일에서 value가 뜨지 않는 까닭은
end 함수 사용했기 때문이다.
만약 stop 함수를 사용하면 메모리 상에는 남아있으나
end 함수를 사용하면 메모리상에서 사용했던 변수의 데이터 값을 지워버린다
////////////////////////////////////

general.pro

pro myplot, x, y, mytitle=plot_title
print, n_params()
case n_params() of
1: plot, x, title=plot_title
2: plot, x, y, title=plot_title
else: print, 'error'
endcase
end

function multiply_value, x, y
return, x*y
end

pro general
degree=findgen(360)
sin_value=sin(degree*!dtor)
curve=multiply_value(sin_value, 3)
myplot, degree, curve, mytitle='OK'
end



general.pro 컴파일후
myplot, findgen(360), findgen(360, mytitle='TEST' 입력하면
결과 화면 출력



* params - 파라미터(인수 값) 몇개?
1개면 x값
2개면 x,y값




반복문(For)

C++에서의 FOR문 역할 동일

pro for_mun
sum=0
hap=0
cak=0
print, 'For_mun test -> i=0~9 i++'
print, '======================'
   for j=0, 9 do begin
    for i= 0, 10 do begin
          sum=sum+i
    endfor
   hap=j*sum
   print, 'Hab1: ', sum
   cak=cak+hap
   endfor
print, 'Hab2:', cak
end


IDL> for_mun

For_mun test -> i=0~9 i++
======================
Hab1:       55
Hab1:      110
Hab1:      165
Hab1:      220
Hab1:      275
Hab1:      330
Hab1:      385
Hab1:      440
Hab1:      495
Hab1:      550
Hab2:   18150


if문 (~~ 인지 아닌지)

< it     > gt     <= le    >= ge    !=  ne    == eq

pro if_then_else, x, y, z
if (x lt 0) then x=0
if (y le 0) then print, -y
  else print, y
if (z eq 0) then begin
print, x
help, y
endif
end

IDL> if_then_else, 0, 0, 1
0
IDL> if_then_else, 99, 99, 99

IDL> if_then_else, 99, 99, 99



/////////////////////////////////
IDL에서 >, < 등의 기호를 사용하지 않는 이유는
>, < 기호가 더 유용하게 사용되기 때문이다.


ex) 

a=indgen(11)
b=a>10
print, b

그렇게 되면
10이하의 수들은 모두 10으로 출력된다.
10 10 10 10 10 10 10 10 10 10 10 10 11
/////////////////////////////////////



While문 (~~ 하는 동안)

pro while_loops, pre, cur
 while (cur lt 100) do begin
    nex = cur + pre
    pre = cur
    cur = nex
 end while
 print, cur
end


Until문



Case 문 ()

pro case_stat, piechoice
 case piechoice of
 0: begin
pie='apple'
topping=ice cream'
end
1: pie='pumpkin'
else: pie='cherry'
endcase
print, pie
end

case_stat, 0
-> apple
case_stat, 1
-> pumpkin
case_stat, 9
-> cherry