Apparently there is no direct method to determine the length of an array in VBScript. The best way I could find is to use UBound(ArrayName). This will return the upper limit of the array. Unfortunately, it doesn’t account for empty items in the array. In the example below Length will be equal to 10, not 1.
Dim NewArray(10)
NewArray(0) = "Apple"
Length = UBound(NewArray) 'Length is equal to 10, not 1
Here is a good link to VBScript Array functions.
http://www.shocknet.org.uk/defpage.asp?pageID=30
If you know of a better way to determine the length, please let me know.
MrBester says
It depends what you mean by “length”.
If you define an array in JavaScript (var arr=[];) and then set the 4th element to something (arr[3]=’something’;) then the length property will report 4 in the same way as VBScript. Are they both wrong?
If you mean length to be “number of non-empty elements” then:
Function Length(ByRef vArray)
Dim llngIndex
Dim llngCount
llngCount = 0
For llngIndex = 0 To UBound(vArray)
If Not(IsNull(vArray(llngIndex))) And Not(IsEmpty(vArray(llngIndex))) And Not(vArray(llngIndex) Is Nothing) Then
llngCount = llngCount + 1
End If
Next
Length = llngCount
End Function
should do the trick, unless you count an element containing vbNullString (or “”) as empty as well, which then gets you into an argument about what “empty” means.
I’ve always worked on the principle that an array length is the number of elements within it, regardless of the content of those elements because that is what properties called “length” return to me.
“Size” rears its ugly head as an even trickier beast. Does “size” mean number of elements, bytelength of contents, memory consumed by the object?
Tom says
Wow – thanks! That’s a good function. You brought up some good points in regards “emptyness.”
I originally was searching for the solution to find array length after using the split command (because you wouldn’t have explicitly defined the length of the array), but this could go along nicely with my script.
Dave S says
You could always create a counter, loop thru the array & increment as you go. It may be ugly but it works
Dim counter
For Each x in myArray
counter = counter + 1
Next
0xG says
What is even more perplexing is how to determine number if elements in a dynamic array. For instance:
Dim Dynamic()
WScript.Echo “Array size is ” & UBound(Dynamic)
>Microsoft VBScript runtime error: Subscript out of range
dummy says
Give your dynamic array a size of 0:
Dim Dynamic(0)
Then it won’t error because it will have a length.
Mike Irving says
The loop counter is very useful, shame there is now built in loop property.
Frederic Duplessis says
Dynamic arrays can NOT be defined as “dummy” was suggesting
Dim dynamic (0)
.In VBScript, the subscript in Dim statement really is what will be returned via UBound function, which is namely the highest subscript one may use to refer to an element in the array and NOT the size of it. Thus this statement
Dim dynamic (0)
would really create an fixed-size, non-dynamic, array variable containing one element accessible with subscript 0.Dave’s solution seems the only avenue here.
chaitanya says
Hi every one,
I need to find the number of bytes occupied by a array variable.Can any one help me please.
chaitanya says
Size means bytelength of content
LAYGO says
Frederic is right.
If you want a true dynamic array:
Dim DynamicArray()
For x = 0 To n
ReDim DynamicArray(x) Preserve
‘ some processing here
DynamicArray(x) = “results of some processing here”
Next
Amar says
Problem in above solution is, every time when Array is redimed previous value will be get flushed out, so only last value will be stored
Alex says
@Amar
Not so. The Preserve keyword on the ReDim keeps all the existing data in the array intact. Although if memory serves me correctly the syntax is with Preserve directly following the ReDim statement.
ReDim Preserve DynamicArray(10)
@0xG
Another way I found of getting around the error generated is to ReDim your dynamic array right after declaring it. That then gives you a ubound to work with from the start.
Dim Dynamic()
ReDim Dynamic(0)
WScript.Echo “Array size is ” & UBound(Dynamic)
You can then go on to do your other processing too as per the example in LAYGO’s loop.