2007년 4월 30일 월요일

정규표현식 활용 1

문구중에서 괄호와 그안에 있는 내용을 제거하는 정규표현식 이다.
정규표현식중에서 많이 사용하는 replace를 함수로 만들어 사용한다.

다양한 정규표현식 예제를 통해 학습해야 한다.


SearchReplace( sText, sPattern , sReplace ) 에서
sText : 입력한 문자열
sPattern : 검색할 정규 표현식 패턴
sReplace : 검색된 문구를 대체할 문자열



InitialString = "we have a good reference (logo : good_refence.pdf)"

WScript.echo InitialString
WScript.echo SearchReplace( InitialString, "\([^\)]*\)", "")


Function SearchReplace( sText, sPattern , sReplace )
Dim RegEx

Set RegEx = New RegExp
RegEx.Pattern = sPattern
RegEx.IgnoreCase = True
RegEx.Global = True

SearchReplace = RegEx.Replace(sText, sReplace)
Set RegEx = Nothing
End Function

2007년 4월 27일 금요일

cint : Integer의 variant 형으로 변환
clng : long의 variant 형으로 변환
csng : single의 variant 형으로 변환
cdbl : double의 variant 형으로 변환
fix : 정수형 부분을 반환
round : Rounds a number (정확한뜻은 모르겠음...)



그런데, 일반적으로 cint와 round를 반올림 함수로 사용하는 경우를 많이 보았다.
나도 그랬다...



반올림이란... 예를들어 소수점 첫번째 자리에서 반올림 한다면..
1.2 는 1.0
1.5는 2.0
1.9는 2.0
으로 나타낼때 반올림 한다고 한다.

실제로 cint를 사용해 보면, 그렇게 반환을 한다.

그러나, 예외의 경우가 있다.
아래의 vb 스크립트를 실행해 보자.



For i = 0 To 5 Step 0.1
wscript.echo "cint("& i &")="& CInt(i) &" | fix("& i &")"& Fix(i)
Next



cint(0)=0 | fix(0)=0 | round(0)=0
cint(0.1)=0 | fix(0.1)=0 | round(0.1)=0
cint(0.2)=0 | fix(0.2)=0 | round(0.2)=0
cint(0.3)=0 | fix(0.3)=0 | round(0.3)=0
cint(0.4)=0 | fix(0.4)=0 | round(0.4)=0
cint(0.5)=0 | fix(0.5)=0 | round(0.5)=0
cint(0.6)=1 | fix(0.6)=0 | round(0.6)=1
cint(0.7)=1 | fix(0.7)=0 | round(0.7)=1
cint(0.8)=1 | fix(0.8)=0 | round(0.8)=1
cint(0.9)=1 | fix(0.9)=0 | round(0.9)=1
cint(1)=1 | fix(1)=0 | round(1)=1
cint(1.1)=1 | fix(1.1)=1 | round(1.1)=1
cint(1.2)=1 | fix(1.2)=1 | round(1.2)=1
cint(1.3)=1 | fix(1.3)=1 | round(1.3)=1
cint(1.4)=1 | fix(1.4)=1 | round(1.4)=1
cint(1.5)=2 | fix(1.5)=1 | round(1.5)=2
cint(1.6)=2 | fix(1.6)=1 | round(1.6)=2
cint(1.7)=2 | fix(1.7)=1 | round(1.7)=2
cint(1.8)=2 | fix(1.8)=1 | round(1.8)=2
cint(1.9)=2 | fix(1.9)=1 | round(1.9)=2
cint(2)=2 | fix(2)=2 | round(2)=2
cint(2.1)=2 | fix(2.1)=2 | round(2.1)=2
cint(2.2)=2 | fix(2.2)=2 | round(2.2)=2
cint(2.3)=2 | fix(2.3)=2 | round(2.3)=2
cint(2.4)=2 | fix(2.4)=2 | round(2.4)=2
cint(2.5)=3 | fix(2.5)=2 | round(2.5)=3
cint(2.6)=3 | fix(2.6)=2 | round(2.6)=3
cint(2.7)=3 | fix(2.7)=2 | round(2.7)=3
cint(2.8)=3 | fix(2.8)=2 | round(2.8)=3
cint(2.9)=3 | fix(2.9)=2 | round(2.9)=3
cint(3)=3 | fix(3)=3 | round(3)=3
cint(3.1)=3 | fix(3.1)=3 | round(3.1)=3
cint(3.2)=3 | fix(3.2)=3 | round(3.2)=3
cint(3.3)=3 | fix(3.3)=3 | round(3.3)=3
cint(3.4)=3 | fix(3.4)=3 | round(3.4)=3
cint(3.5)=4 | fix(3.5)=3 | round(3.5)=4
cint(3.6)=4 | fix(3.6)=3 | round(3.6)=4
cint(3.7)=4 | fix(3.7)=3 | round(3.7)=4
cint(3.8)=4 | fix(3.8)=3 | round(3.8)=4
cint(3.9)=4 | fix(3.9)=3 | round(3.9)=4
cint(4)=4 | fix(4)=4 | round(4)=4
cint(4.1)=4 | fix(4.1)=4 | round(4.1)=4
cint(4.2)=4 | fix(4.2)=4 | round(4.2)=4
cint(4.3)=4 | fix(4.3)=4 | round(4.3)=4
cint(4.4)=4 | fix(4.4)=4 | round(4.4)=4
cint(4.5)=4 | fix(4.5)=4 | round(4.5)=4
cint(4.6)=5 | fix(4.6)=4 | round(4.6)=5
cint(4.7)=5 | fix(4.7)=4 | round(4.7)=5
cint(4.8)=5 | fix(4.8)=4 | round(4.8)=5
cint(4.9)=5 | fix(4.9)=4 | round(4.9)=5
cint(5)=5 | fix(5)=4 | round(5)=5


0.5 가 반올림 소수점 첫번째 자리에서 반올림을 하면, 1 이 되어야 하는데,
0 이 반환 되었다. 따라서, cint, round 를 반올림함수로 사용하게 될경우
문제가 발생할 경우가 있다는 뜻이다. 4.5도 마찬가지...


그렇다면, 반올림 함수를 사용하려면? 다음과 같이 함수를 만들어 사용해야 한다.


Function roundup( nIn )
Dim nPar, nChi

nPar = Fix(nIn)
nChi = nIn - nPar
If nChi >= 0.5 Then
roundup = nPar + 1
Else
roundup = nPar
End If
End Function



그렇다면, 지금 만든 roundup으로 반올림을 해보자.

cint(0)=0 | roundup(0)=0 | round(0)=0
cint(0.1)=0 | roundup(0.1)=0 | round(0.1)=0
cint(0.2)=0 | roundup(0.2)=0 | round(0.2)=0
cint(0.3)=0 | roundup(0.3)=0 | round(0.3)=0
cint(0.4)=0 | roundup(0.4)=0 | round(0.4)=0
cint(0.5)=0 | roundup(0.5)=1 | round(0.5)=0
cint(0.6)=1 | roundup(0.6)=1 | round(0.6)=1
cint(0.7)=1 | roundup(0.7)=1 | round(0.7)=1
cint(0.8)=1 | roundup(0.8)=1 | round(0.8)=1
cint(0.9)=1 | roundup(0.9)=1 | round(0.9)=1
cint(1)=1 | roundup(1)=1 | round(1)=1
cint(1.1)=1 | roundup(1.1)=1 | round(1.1)=1
cint(1.2)=1 | roundup(1.2)=1 | round(1.2)=1
cint(1.3)=1 | roundup(1.3)=1 | round(1.3)=1
cint(1.4)=1 | roundup(1.4)=1 | round(1.4)=1
cint(1.5)=2 | roundup(1.5)=2 | round(1.5)=2
cint(1.6)=2 | roundup(1.6)=2 | round(1.6)=2
cint(1.7)=2 | roundup(1.7)=2 | round(1.7)=2
cint(1.8)=2 | roundup(1.8)=2 | round(1.8)=2
cint(1.9)=2 | roundup(1.9)=2 | round(1.9)=2
cint(2)=2 | roundup(2)=2 | round(2)=2
cint(2.1)=2 | roundup(2.1)=2 | round(2.1)=2
cint(2.2)=2 | roundup(2.2)=2 | round(2.2)=2
cint(2.3)=2 | roundup(2.3)=2 | round(2.3)=2
cint(2.4)=2 | roundup(2.4)=2 | round(2.4)=2
cint(2.5)=3 | roundup(2.5)=3 | round(2.5)=3
cint(2.6)=3 | roundup(2.6)=3 | round(2.6)=3
cint(2.7)=3 | roundup(2.7)=3 | round(2.7)=3
cint(2.8)=3 | roundup(2.8)=3 | round(2.8)=3
cint(2.9)=3 | roundup(2.9)=3 | round(2.9)=3
cint(3)=3 | roundup(3)=3 | round(3)=3
cint(3.1)=3 | roundup(3.1)=3 | round(3.1)=3
cint(3.2)=3 | roundup(3.2)=3 | round(3.2)=3
cint(3.3)=3 | roundup(3.3)=3 | round(3.3)=3
cint(3.4)=3 | roundup(3.4)=3 | round(3.4)=3
cint(3.5)=4 | roundup(3.5)=4 | round(3.5)=4
cint(3.6)=4 | roundup(3.6)=4 | round(3.6)=4
cint(3.7)=4 | roundup(3.7)=4 | round(3.7)=4
cint(3.8)=4 | roundup(3.8)=4 | round(3.8)=4
cint(3.9)=4 | roundup(3.9)=4 | round(3.9)=4
cint(4)=4 | roundup(4)=4 | round(4)=4
cint(4.1)=4 | roundup(4.1)=4 | round(4.1)=4
cint(4.2)=4 | roundup(4.2)=4 | round(4.2)=4
cint(4.3)=4 | roundup(4.3)=4 | round(4.3)=4
cint(4.4)=4 | roundup(4.4)=4 | round(4.4)=4
cint(4.5)=4 | roundup(4.5)=5 | round(4.5)=4
cint(4.6)=5 | roundup(4.6)=5 | round(4.6)=5
cint(4.7)=5 | roundup(4.7)=5 | round(4.7)=5
cint(4.8)=5 | roundup(4.8)=5 | round(4.8)=5
cint(4.9)=5 | roundup(4.9)=5 | round(4.9)=5
cint(5)=5 | roundup(5)=5 | round(5)=5

2007년 3월 25일 일요일

For Each In

For Each 변수이름 In 컬렉션
이벤트
Next

주목해야 할건, 컬렉션과, 변수이름이다.

예제1) ServerVariables 값을 모두 출력하기

For Each svName In request.servervariables

response.write svName & " : " & request.servervariables(svName)
Next

예제2) FSO를 활용하여, 해당 파일이 있는 폴더의 모든 파일 리스트 구하기

Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(Server.Mappath("."))
Set files = folder.Files

For Each file in Files
Response.Write file.Name & "
"
Next

Set files = Nothing
Set folder = Nothing
Set fso = Nothing

예제1) 예제2)에 있는 svName 이나, file 대신 다른 변수명을 써도 된다는 사실을 꼭 알아야 할것이다. 예제2)에 나왔던, FSO는 추후에 다시 공부하자