SortArray() [Sort single dimension array].vbs |
This example shows how you can pass an array to a function or subroutine and have the function update it (note that "ByRef" is used).
Note that a subroutine taking an array as a parameter must not be called using brackets as a VBSCRIPT bug prevents the updated array being passed back (it acts like it was passed "Byval"!
The Code for: "SortArray() [Sort single dimension array].vbs" |
This is the example, a shortcut was installed to this code :
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ' $Header: C:/DBAREIS/Projects.PVCS/Win32/ScriptingTipsAndTricks/EXAMPLE[vbs].GetEnv() [Get Environment Variables].vbs.txt.pvcs 1.0 11 Jul 2014 19:31:06 USER "Dennis" $ '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ '--- BEFORE ----------------------------------------------------------------- dim SomeText : SomeText = array("BBB", "AAA", "ZZZ", "GGG") for i = 0 to ubound(SomeText) wscript.echo "UNSORTED: " & SomeText(i) next wscript.echo "" '--- SORT THE ARRAY. NOTE: VBSCRIPT FAILS IF: SortArray(SomeText) ------- SortArray SomeText '--- AFTER ------------------------------------------------------------------ wscript.echo "" for i = 0 to ubound(SomeText) wscript.echo "SORTED : " & SomeText(i) next wscript.quit 0 '============================================================================ sub SortArray(ByRef ArrayName) '============================================================================ on error resume next dim SortI, SortJ, SortTmp for SortI = 0 to ubound(ArrayName) for SortJ = SortI+1 to ubound(ArrayName) if ArrayName(SortI) > ArrayName(SortJ) then SortTmp = ArrayName(SortJ) ArrayName(SortJ) = ArrayName(SortI) ArrayName(SortI) = SortTmp end if next next end sub