# Testing for Empty Arrays

This guide discusses how to determine a VBScript array is empty.

## Problem

It is often necessary to analyze an array to determine whether or not it is empty; that is, if it has any space to store elements. Consider the following test…

Sub Main()
Dim arr
arr = Array()
If IsArray(arr) Then
Rhino.Print("This should not print")
End If
End Sub


…which does not seem to work.

## Solution

When you execute this statement:

arr = Array()


you are declaring an array that has an upper bounds of -1. Because this variable is an array, it will pass the IsArray() test. What the above code needs is an additional test condition to see if the upper bounds of the array is greater than -1:

Sub Main()
Dim arr
arr = Array()
If IsArray(arr) And UBound(arr) >= 0 Then
Rhino.Print("This should not print")
End If
End Sub


Now the code works as expected. But, what if the code looked like this?

Sub Main()
Dim arr()
If IsArray(arr) And UBound(arr) >= 0 Then
Rhino.Print("This should not print")
End If
End Sub


Notice that the above code gives you an “Script out of range: UBound” error. This is because, although the variable is an array, it has not been dimensioned. Thus the call to UBound() fails. So, we need a better test - one that will test for both types of array declarations. Consider the following function:

Function IsArrayDimmed(arr)
IsArrayDimmed = False
If IsArray(arr) Then
On Error Resume Next
Dim ub : ub = UBound(arr)
If (Err.Number = 0) And (ub >= 0) Then IsArrayDimmed = True
End If
End Function


Notice how the function above provides error checking. If an error occurs when calling UBound(), it is caught. Thus, the function knows when an array has not been dimensioned. Also, if UBound() returns a value of -1, we know that the array has no space to store elements. We can test this with the following function:

Sub Main()
Dim arr0
Dim arr1()
Dim arr2(3)
Dim arr3 : arr3 = Array()
Dim arr4 : arr4 = Array(1,2,3)
Rhino.Print "Arr0 dimmed = " &  IsArrayDimmed(arr0)
Rhino.Print "Arr1 dimmed = " &  IsArrayDimmed(arr1)
Rhino.Print "Arr2 dimmed = " &  IsArrayDimmed(arr2)
Rhino.Print "Arr3 dimmed = " &  IsArrayDimmed(arr3)
Rhino.Print "Arr4 dimmed = " &  IsArrayDimmed(arr4)
End Sub
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.42880478501319885, "perplexity": 6071.500626873265}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2018-17/segments/1524125948950.83/warc/CC-MAIN-20180427021556-20180427041556-00440.warc.gz"}