æ代é ãã²ã¨ãFizzBuzzç¥ããä»åã¯VBScript on WSHãååã®JScriptã¨ã®ç¹ããã¯ãè¨ãã¾ã§ããªãã ãã*1ã
WSHã®è¨èªã¨ããã¨ä½æ ãVBScriptã®ã¤ã¡ã¼ã¸ãå¼·ãããããä¸ã§WSHã®ãµã³ãã«ãæ¢ãã¨å¤§æµVBScriptã§æ¸ããã¦ãããããã ãã§ãªããCOMãªãã¸ã§ã¯ãçµç±ã§è²ã ãªæ©è½ã使ããã¨ããæã«VBScriptã§ããããã¢ã«ä½¿ããªããã¨ãããã以åWindows Server 2003 R2ä¸ã§ADSIã使ã£ãJScriptã®ã¹ã¯ãªãããåãããã¨ãã¦ãã³ã¬ã¯ã·ã§ã³ãèãããã¨ãã§ããªãã¦æ«æãããã¨ããã£ããããåã«ããæ¹ãæªãã£ãã ããããããªããã©ãVBScriptã®For Each ã Nextã§èãããããªã³ã¬ã¯ã·ã§ã³ãJScriptã§Enumeratorãªãã¸ã§ã¯ãç¡ãã§ã©ãèããã°ããã®ã*2ã
VBScriptã®è¨èªèªä½ã®æ©è½ã«ã¯æå¤ã¨èå³æ·±ãç¹ããããã·ã³ãã«ãªææ³*3ãåçåãç¶æ¿ã®ç¡ãåç´ãªã¯ã©ã¹æ©æ§ãEvalãããã·ã¼ã¸ã£ããªãã¸ã§ã¯ãã¨ãã¦æ±ããã¨ãã§ãã*4ãããªããã£ããªãããã¯ãããã®ã®ãçµã¿è¾¼ã¿é¢æ°ããããªãã«ä¸éãæã£ã¦ãããããã¦ä½ããJScriptã¨åæ§ã«COMãã¼ã¹ã®ãµã¼ãã¹ã容æã«æ±ããã¨ãã§ããã
ãã æãããã¨ã«VBScriptã®è¨èªæ¬ä½ã«ãã©ã¼ã«ã¹ããè³æã¨ããã¨MSDNã®ã¦ã¼ã¶ã¼ãºã¬ã¤ãããªãã¡ã¬ã³ã¹ã®ãããªå½¢å¼ã®ãã®ããè¦ã¤ãããªãããä¸ã¤æ°ãå°ãªããä»ã¯ãVBScript + WSH + COMã§ãããªãã¨ãã§ããï¼ãã¿ãããªå 容ã°ããã ã
COMã§å ¬éããã¦ãã便å©ãªæ©è½ã使ã£ã¦è²ã ãªãã¨ãç°¡åã«å®ç¾ã§ããã®ã¯ç´ æ´ãããã®ã ããããã«æ³¨ç®ãããä½ãã«ä»ã®é¨åããããªãã«ããã¦ããå°è±¡ãåããã®ã ããããä¸ã®VBScriptã§æ¸ããããµã³ãã«ã³ã¼ãã«ã¯è³ªãæªããã®ãå¤ãã®ã ããVBScriptãæ¬è·ã®äººã»ã©ä½¿ã£ã¦ããªãç§ããè¦ã¦ãããã®æ©è½ã使ãã°ããã®ã«â¦â¦ãã¨ããææ³ãæã¤ãããªãã®ã ã£ããããã
VBScriptã®ç¾ç¶ã«ã¤ãã¦å人çã«è²ã ã¨æãã¨ãããããã®ã ããããã§ããããªãã«æå¹ãªè¨èªã§ãããã¨ã«å¤ããã¯ãªããååã®JScriptã¨åæ§ã«ãWindows XP Professional SP3ä¸ã®WSH 5.7ã§åããã¦ã¿ãã
ã¾ãã¯é¢æ°ãå®ç¾©ãã¤ã¤ãç§ãªãã«æ¨æºã ã¨æãããã¹ã¿ã¤ã«ã§ããªã¼ã½ããã¯ã¹ãªFizzBuzzãæ¸ãã¦ã¿ãã
Option Explicit Private Sub Print(ByVal str) WScript.Echo str End Sub Private Function FizzBuzz(ByVal n) If n Mod 15 = 0 Then FizzBuzz = "FizzBuzz" ElseIf n Mod 3 = 0 Then FizzBuzz = "Fizz" ElseIf n Mod 5 = 0 Then FizzBuzz = "Buzz" Else FizzBuzz = CStr(n) End If End Function Private i For i = 1 To 100 Print FizzBuzz(i) Next
ãOption Explicitãã¯å¥ã¨ãã¦ãPrivateãByValã¨ãã£ããã¼ã¯ã¼ãã使ããã¦ãããµã³ãã«ã³ã¼ãã¯ä½ãè¦ããã¨ããªããã¯ã©ã¹ã®å¤ã§Privateã§å®£è¨ããå¤æ°ã¯ãã®ã¹ã¯ãªããå ã§ã®ã¿åç §ã§ãããã¨ãããã¨ãããã®ã ããå¤åWSFã使ãå ´åã«å¹æãçºæ®ããã®ã ããã
ä¸è¦ã§ã¯ããããCStr()ã§æ示çã«æåååï¼ã®ããªã¢ã³ãåï¼ã«å¤æãã¦ããã
VBScriptã«ãæååé£çµã®æ¼ç®åãåå¨ããã`&'ãããã ã
Option Explicit Private Sub Print(ByVal str) WScript.Echo str End Sub Private Function FizzBuzz(ByVal n) Dim str str = "" If n Mod 3 = 0 Then str = "Fizz" If n Mod 5 = 0 Then str = str & "Buzz" If str = "" Then str = n FizzBuzz = str End Function Private i For i = 1 To 100 Print FizzBuzz(i) Next
æååé£çµã®æ¼ç®åã¯è¨èªã«ãã£ã¦ä½¿ç¨ããè¨å·ãç°ãªãã®ã§ãå°ãèå³æ·±ãã
PerlãRubyãPythonãªã©ã«æ¯ã¹ãã¨VBScriptã®æ©è½ã¯å°ã ããªããã£ããªå¾åã«ããã¨æããç¹ã«ç®ç«ã¤ã®ãé åã ãPush/Popã¨ãã£ãé åãµã¤ãºãæ°ã«ããã«æä½ãããã¨ãã§ããé¢æ°ãç¨æããã¦ããªãã®ã§ãCè¨èªã§malloc/reallocãããããªæãã§å¸¸ã«é åã®å¤§ãããæ°ã«ãã¦ãããªãã¦ã¯ãªããªããæ´ã«ã¯é åã®ãªãã©ã«è¡¨è¨ãåå¨ããªãã
Option Explicit Private Sub Print(ByVal str) WScript.Echo str End Sub Private Function FizzBuzz(ByVal n) Dim refTbl, fizz, buzz refTbl = Array(n, "Fizz", "Buzz", "FizzBuzz") fizz = 0 buzz = 0 If n Mod 3 = 0 Then fizz = 1 If n Mod 5 = 0 Then buzz = 2 FizzBuzz = refTbl(fizz + buzz) End Function Private i For i = 1 To 100 Print FizzBuzz(i) Next
é åã®ãªãã©ã«è¡¨è¨ããªã代ããã«çµã¿è¾¼ã¿é¢æ°ã®Arrayã使ããã¨ã«ãªããããã®ã¯å¬ããã®ã ãã便å©ãªã®ãä¸ä¾¿ãªã®ãã¤ãã¤ãåããã«ããæããã
ããã·ã¥ï¼é£æ³é åã¨ãã£ãæ©è½ã¯çµã¿è¾¼ã¾ãã¦ããªãã®ã§ãCOMãªãã¸ã§ã¯ãã使ããã¨ã«ãªãã
Option Explicit Private Sub Print(ByVal str) WScript.Echo str End Sub Private referenceTable Set referenceTable = WScript.CreateObject("Scripting.Dictionary") referenceTable.Add 0, 0 referenceTable.Add 1, "Fizz" referenceTable.Add 2, "Buzz" referenceTable.Add 3, "FizzBuzz" Private Function FizzBuzz(ByVal n) Dim fizz, buzz referenceTable(0) = n fizz = 0 buzz = 0 If n Mod 3 = 0 Then fizz = 1 If n Mod 5 = 0 Then buzz = 2 FizzBuzz = referenceTable(fizz + buzz) End Function Private i For i = 1 To 100 Print FizzBuzz(i) Next Set referenceTable = Nothing
ãScripting.Dictionaryããªã®ã ããå人çã«ãªãã©ã«è¡¨è¨ãç¨æããã¦ããªãç¹ã使ãé£ãã¨æãããããã§ãå¨ãã ããã·ã ããé åã¨éã£ã¦ã¡ã½ããAddã§å¤§ãããæ°ã«ããã«è¦ç´ ã追å ã§ããã®ã§ãç°¡æçã«é åã®ä»£ããã«ä½¿ããã¨ãã§ããã
ãã¾ãç¥ããã¦ããªããããããªãããVBScriptã§ãããã·ã¼ã¸ã£ï¼ã®åç §ï¼ããªãã¸ã§ã¯ãã¨ãã¦æ±ããã¨ãã§ãããèªåã§å®ç¾©ããé¢æ°ã«å¯¾ãã¦çµã¿è¾¼ã¿é¢æ°GetRefãé©ç¨ããã°ããã
Option Explicit Private Sub Print(ByVal str) WScript.Echo str End Sub Private Function FizzBuzz(ByVal n) Dim str str = "" If n Mod 3 = 0 Then str = "Fizz" If n Mod 5 = 0 Then str = str & "Buzz" If str = "" Then str = n FizzBuzz = str End Function Private Function MakeArray(ByVal size, ByVal initFunc) Dim ary() ReDim ary(size - 1) Dim i For i = 0 To UBound(ary) ary(i) = initFunc(i) Next MakeArray = ary End Function Private Function FizzBuzzWrapper(ByVal n) FizzBuzzWrapper = FizzBuzz(n + 1) End Function Private fizBuzAry, val fizBuzAry = MakeArray(100, GetRef("FizzBuzzWrapper")) For Each val in fizBuzAry Print val Next
ãããªæãã ãé£ç¹ã¨ããã°ãVBScriptã«ã¯ç¡åé¢æ°ã®é¡ãç¨æããã¦ããªãã®ã§ãå¿ ãããã·ã¼ã¸ã£ãå®ç¾©ãã¦ããå¿ è¦ããããã¨ããæã ããããã£ã¨ãCããã°ã©ãè¦ç¹ã§ã¯ãé¢æ°ãã¤ã³ã¿ã£ã½ããããã¨ãããã¨ã§è¦ã«ã¯ãªããªãã®ã ããä»ã®è¨èªãç¥ã£ã¦ãã身ã¨ãã¦ã¯å°ã ä¸æ°å¥½ã ã¨ããå°è±¡ãæããããªãã
ã¨ããã§ãå²ã¨ç¥ããã話ã ã¨æããã.NET Frameworkãã¤ã³ã¹ãã¼ã«ããã¦ããå ´åããªãã¸ã§ã¯ãã«ãã£ã¦ã¯VBScriptã§ä½¿ç¨ã§ããããããã¨ããã®ãæ±ç¨çã«ä½¿ããããªã¯ã©ã¹ãCOMãªãã¸ã§ã¯ãã¨ãã¦ç»é²ããã¦ããã®ã ã
Option Explicit Private Sub Print(ByVal str) WScript.Echo str End Sub Private Function FizzBuzz(ByVal n) Dim str str = "" If n Mod 3 = 0 Then str = "Fizz" If n Mod 5 = 0 Then str = str & "Buzz" If str = "" Then str = n FizzBuzz = str End Function Private fizBuzAry, i, val Set fizBuzAry = CreateObject("System.Collections.ArrayList") For i = 1 To 100 fizBuzAry.Add FizzBuzz(i) Next For Each val in fizBuzAry Print val Next
ãããä¸ã®ãµã³ãã«ã¨åæ§ã«ãSystem.Collections.ArrayListã使ç¨ãã¦ã¿ããVBScriptã®é åã¯ãæ£ç´ãªæJScriptãªã©ã«æ¯ã¹ãã¨æ©è½ãåå§çã§ä½¿ãè¾ãã®ã§ããããªããã¯èªåç¨ã®ãã¼ã«ã«ä½¿ãåã«ã¯éå®ãããããããªãã
Windowsã§ã®ã¹ã¯ãªããè¨èªã¯PowerShellã«ç§»è¡ãã¦ããã ããããä¾ç¶ã¨ãã¦VBScriptã¯ç¾å½¹ã ãããè¨èªèªä½ãçµæ§ä¾¿å©ã§ä½¿ããããããä½ããç¾è¡ã®Windowsä¸ã§ããã©ã«ãã§ä½¿ç¨ã§ããã¡ãªããã¯å¤§ãããæ©è½ãå°ã ããªããã£ãã§ã¯ãããããã®è¾ºãã¯ã©ã¤ãã©ãªãæµãããªã.NET Frameworkã®æ©è½ãåãããªãããã°è§£æ±ºã§ããã¯ãã ã
ããããããã¾ã§æ¸ãã¦ãã¦ã¢ã¬ã ãããã¯ãç§ã¨ãã¦ã¯JScriptã®æ¹ã好ãã ãVBScriptã¯æªããªãã®ã ããã©ãããã£ããããªãã
*1:ããã©ã«ãã§WSH対å¿ãªããã°ã©ãã³ã°è¨èªç¹ããã
*2:ADSIã§åå¾ããã³ã¬ã¯ã·ã§ã³ãEnumeratorã§èãããã¨ããããããã¤ã«ã¯æªå¯¾å¿ã ãã¨è¨ããã¦åºæ¥ãªãã£ãã
*3:Visual Basicã®æ©è½ã®å¹¾ã¤ãã¯ãVBScriptã§ã¯çç¥ããã¦ããã
*4:æ£ç¢ºã«ã¯ããã·ã¼ã¸ã£ã¸ã®åç §ããã ãã