VBScript itself needs to be used with an environment providing resources, mostly I/O, to work with. Environments include Web pages, Windows Scripting Host, and programs such as Excel and Word.
Notes
DosBoxPrompt> CScript //? DosBoxPrompt> WScript //? Command Line : Wscript.Echo '' not Cscript.Echo ? Web Script : document.write Continuation : underline _ Option Explicit '' To require declarations
It appears that PowerShell is intended to take the place of VBScript - Install UK. News:microsoft.public.windows.powershell exists.
These VBScript pages are largely about VBScript as used in Web pages; but, in WSH, CScript and WScript are rather similar. I do not know much about these languages yet - there is a little in my MS-DOS Batch Files.
Until 2006-10-14, I used Win98 1st Edn IE4. From 2006-10-19, I used Win XP sp2 IE6; from 2008-03-18, IE7; from 2008-07-06, XP sp3; from 2009-07-21, IE8.
Much of this site was written in IE4, but sometimes checked in later browsers.
My IE4 did not have VBScript RegExps; accordingly, there may be code in these VBS pages which could be better done with RegExps. Material in JavaScript RegExps & Validation will be applicable in part to VBScript.
The examples given here all work, obviously, in a Web page (or, at least, they do so locally on my system); some of the algorithms have been tested at the command line. Use View Source. Test carefully before serious use.
Notes
'' : Comment quote is doubled here, to avoid confusing Tidy
See also JavaScript Index & Introduction and Pascal Introduction, for code that could be translated to VBScript.
VBScript is invoked as the first scripting on this page → ←.
Therefore, even though some later code on the page could be JavaScript, VBScript will be used for all event handlers. For convenience, Option Explicit is preset in that script.
MH wrote : You can disambiguate by using onsomeeventname='javascript:foo();' onsomeeventname='vbscript:call foo()' onsomeeventname='vbscript:foo'
I have found no means in VBScript of displaying the code of a function given only its name; therefore, I have no equivalent of the colour-bordered boxes that I use in JavaScript. Here, copies of blocks of code to be executed, and their output, will be colour-coded as follows :-
document.write "The result of the code" '' is below
Other output is
To be certain about just what code is executed, view the source code of the page.
Code fragments - VBScript, JavaScript, HTML - can be tested as in JavaScript/HTML/VBS Quick Trials
D = Now '' A sample code fragment
You are using
function Widen(X, L) '' X, to width L Widen = Space(L-Len(X)) & X end function
Regular Expressions became available, I believe, at VBS Version 5.
In recent systems, VBScript (in Web pages) and VBS (for execution at a Windows MS-DOS Prompt by CSCRIPT or in the GUI by WSCRIPT) have RegExps.
I acquired IE6 (from 2006-10-19) and now use IE8, but have scarcely started using VBScript RegExps.
Floating-point numbers have the usual potential for rounding errors, as in JavaScript Maths.
In your browser,
X = 1/7 : document.write 1.0-(X+X+X+X+X+X+X)
writes
I get 2.22044604925031E-16 = 2-52.
The actual value is evidently held in binary as an IEEE Standard 754 number of type Double. See Pascal Floating-Point for links to general information on floating-point numbers and arithmetic, and Pascal / Delphi / + Types for data on IEEE number representation, and JavaScript Maths.
FormatNumber(X, n) FormatCurrency(X, n) '' Uses Windows currency sign
Those convert X to a string with n decimal places.
I suspect that both "." and "," are possible, depending on location setting.
Randomize alone loads the generator seed from the Timer function; it should, almost always, be used only once per program, such as at the beginning. Note that Timer has a resolution of 0.01 s, and may be updated at only 18.2 Hz. The generator is only 24-bit.
Randomize N uses N to load the seed (range of N ?), and can be used as required.
Function Rnd should give results in the range 0 to 1. Try something like :-
Randomize 5 for K = 1 to 15 R = Rnd document.writeln R, " ", R*16777216 '' 2^24 next
The generator is said to use Xn+1 = ( Xn × 16598013 + 12820163 ) mod 16777216 and the result of function Rnd is that divided by 16777216. That will be confirmed if the integer part of the following output (calculated in JavaScript by that expression in your browser) matches the previous.
Presuming that 0 ≤ Rnd < 1.0 :-
function Random(N) '' Return a random integer in 0..(N-1) Random = Int(N*Rnd) end function Dim K Randomize for K = 1 to 50 document.write Random(5) next
Gives for N = 5 :-
This is the efficient Fisher-Yates / Durstenfeld / Knuth shuffle. Use menu View Source to see the code. The core of it is :-
For K = 1 to L : J = L-K X = Random(J+1) : T = A(J) : A(J) = A(X) : A(X) = T '' Swap A(X) A(J)
See also JavaScript Random and Borland Pascal/Delphi Random, for code that could be translated to VBScript, including for shuffle, deal, and draw.
Donald E Knuth :- "Random numbers should not be generated with a method chosen at random".
ArcTan2(Y, X) gives the angle measured anti-clockwise from the line Y=0 to the point X, Y in radians.
This tests one of two (see source) functions for ArcTan2(Y, X) which appears not natively available (an untested Pascal version is in Pascal Maths) :-
One can determine ArcTan2(Y, X) as 2*Atn(Fn(Y, X)) for some Fn(Y, X) which seems elegant. The results of the two functions differ only by O(LSB).
Obviously the string S can be split and used to index an array of things-to-be-combined-or-permuted. One may prefer to replace the write by something that adds S as a new element of an array, or to replace S & J similarly.
The implementations are not necessarily optimum.
Each element is either present or absent; elements occur in order. Note where the first output line represents the empty set.
function Comb(M, N, S) dim J document.writeln S, "." for J = M to N : call Comb(J+1, N, S & J) : next end function call Comb(0, 3, "Comb ")
The algorithm is to select an element from those as yet unchosen, and then either to stop or to select from those higher ones remaining.
All elements are present; all orders occur.
function Perm(A, M, N, S) dim J if M<0 then document.writeln S, "." for J = 0 to N if A(J) then A(J) = False : call Perm(A, M-1, N, S & J) : A(J) = True next end function dim A(9), K for K = 0 to 9 : A(K) = True : next call Perm(A, 2, 2, "Perm ")
The algorithm is to recursively select an element from those as yet unchosen. Another approach would be to take all possible orders of each combination. Remove M to get all ordered selections.
While script size and speed may often not be of much importance, it is good to use the best method, and instructive to investigate what that might be in various cases.
It is often necessary to convert a Number to a fixed-length string with leading zeroes. This compares
St = Right(10000+K,4) ' Add St = Right("000"&K,4) ' Cat
In IE4, I found Add to be on average a little faster than Cat; and similarly in WSH cscript. ?????
It is often necessary to convert a CDate to a string YYYYMMDD. This compares
St = CStr(Y*10000+M*100+D) ' Add St = Y & Right("0"&M, 2) & Right("0"&D, 2) ' Cat
In IE4, I found Add to be on average about 2.7 times faster than Cat; and similarly in WSH cscript.
Interestingly, using 1e4,1e2 instead of 10000,100 is much slower
It is often necessary to convert a CDate to a string hhmmss. This compares
St = Right(1000000+h*10000+m*100+s, 6) ' Add St = Right("0"&h, 2) & Right("0"&m, 2) & Right("0"&s, 2) ' Cat
??? In IE4, I found Add to be on average about 3.1 times faster than Cat; and similarly in WSH cscript.
This is cut down from JavaScript/HTML/VBS Quick Trials. Give VBScript in the textarea, using document.write or other means of observing its effect.