Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine if Array is empty 1

Status
Not open for further replies.

BG12424

Programmer
Jun 4, 2002
717
US
What is the syntax to determine if there is any content in an array? Thanks regards,
Brian
 
If strMyArray(1) = "" then
'Array empty
else
'not empty
End If
www.vzio.com
ASP WEB DEVELOPMENT



 
You could try the isEmpty function, but I have heard mixed reviews on it's effectiveness.

If isEmpty(myArray)
Response.Write "It's as empty as empty could be"
Else
Response.Write "This glass is half full"
End If

-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
For my next trick I will pull a hat out of a rabbit (if you think thats bad you should see how the pigeon feels...) :p
 
I read that isEmpty determines whether the variable has been initialized.
www.vzio.com
ASP WEB DEVELOPMENT



 
I have an array that I never need to destroy or deinitialize, so would this work?

if not IsArray(arrListCm) then
create the array
else
array is there and use the values from it
end if

OR, is this a bad way to do it?

In the example above,

If strMyArray(1) = "" then
'Array empty
else
'not empty
End If

is the strMyArray(1) checking for data in the first dimension? Thanks

regards,
Brian
 
be careful of using the If strMyArray(1) = "" condition to determine if your array is empty.

first, this is actually checking the second element in the array. remember that arrays are zero-based! so if you really wanted to do this, it would have to be written as If strMyArray(0) = ""

and the second reason to be careful using this, is that an empty string in one element doesn't necessarily mean that all other elements are empty too!

to answer your question, i would be curious how the array is getting created and populated. i used to use a trick where i would originally create the array with dimension -1, i.e., Dim myArray(-1), and would then REDIM PRESERVE the array one element larger when needing to add anything to it. (not a good tactic on very large arrays, but works well with smaller arrays.) that way, i could check the UBound of the array, and if it returned -1, i would know that nothing got entered into it.

p.s., this is all for one-dimensional arrays, but the theory works just the same for any n-dimensional array.

if you could shed some more light on your needs, we can expand on that. you may be able to use something as simple as a flag, that you can set to TRUE every/anytime you enter data into the array, so that if this flag is FALSE it means nothing ever got entered.

good luck!
 
Here is the code that I am using to populate an array. I am then storing this array in cache using the application object variable.


'get list of all commodities
arrListAll = application("CmListAll")
if not isarray(arrListAll) then
'load cache from database
recs=0
WV_CmListAll recs
Total_Records = recs
arrListAll = application("CmListAll")
else
'get data from cache
Total_Records = ubound(arrListAll,2) + 1
end if

function WV_CmListAll(recs)
' replace qry_WVCmListAll.cfm

dim cmd, rs, dbConn, arrListAll

Set dbConn = Server.CreateObject("ADODB.Connection")
Set cmd = Server.CreateObject("ADODB.Command")
Set rs = Server.CreateObject("ADODB.Recordset")

dbConn.Open strConn

with cmd
.ActiveConnection = dbConn
.CommandText = "WVCmListAll"
.CommandType = adCmdStoredProc
Set rs = .Execute
end with

'using the GetRows() method of the recordset object is much faster at producing output
'we are placing the records in a 2-dimensional array and cached in the application object variable
'for later use.
if rs.State = adStateOpen then
arrListAll = rs.GetRows()
recs = ubound(arrListAll,2) + 1
application("CmListAll") = arrListAll
CloseRS(rs)
end if

CloseDBConn(dbConn)

end function regards,
Brian
 
What if I just Dim'ed the array?

what methods out there do I have to test if the array has values or not?
Code:
Dim myArray()
.
.
some user functions... but the user never used the array
.
.
.
if isEmpty(aryTest) then...
the isEmpty, isNull both return "false" when I try to use them to test if the array has been used. If I try Ubound, it errors out...


Earnie Eng
 
personally testing my arrays, i use the folling steps in order to safeguard from errors and also to be overcautious in some scenarios :

IsArray(MyArr) ' make sure it is an array
IsEmpty(MyArr) ' make sure of definition (slots)
Ubound(MyArr) < 0 ' make sure theres at least 1 slot
MyArr(0) <> "" ' check the first location


[thumbsup2]DreX
aKa - Robert
if all else fails, light it on fire and do the happy dance!
 
As has been pointed out above, it's possible for the 0 element to be empty while the rest of the array could be full of stuff.

One quick test might be to use the Join command, like this:
Code:
If IsArray(MyArray) And Len(Join(MyArray, "")) > 0 Then
    'It's an array with something in it
Else
    'Either it's not an array or it's an empty one
End If
 
Despite my answer 2 years ago, I don't think IsEmpty will work on arrays ever.
The actual array variable (once Dim'd) is basically a memory reference pointer, so by the very act of Dim'ing it you are giving it a value, whether or not you give the memory locations it is pointing to values. Basically the IsEmpty function checks the VarType for a variable and returns truif the type is 0 (uninitialized). An array, by definiton, has to be initialized as an array type, even if it has no values or indexes.

Unfortunately, trying to UBound an array that has not had bounds specfied yet will throw an error, so attempting to determine if an array declred without bounds has been used is a little difficult.

Basically the quickest method you could possibly use to test your array would be to try and force an error:
Code:
Function HasValues(testArray)
	On Error Resume Next
	If IsEmpty(testArray(LBound(testArray))) And IsEmpty(testArray(UBound(testArray))) Then
		HasValues = False
	Else
		HasValues = True
	End If
	On Error Goto 0
End Function

This example will catch pretty much everything I can think of. The only failures would be if you (for some reason) never set values for the lowest or highest index, but used the rest of the array.

It should catch arrays that have not been initialized with bounds, arrays that have been initialized with bounds and no values given to first/last index, variables that are not arrays, etc.

The only time it will return true is when the first or last index has been assigned a value (even if it is an emoty string, etc). The rest of the time it will return false.

I don't know how this compares to the Jopin method, but I would be wary of using that on arrays that could be large string arrays, simply because you don't want to eat up all that memory simply to test that you used the array.

-T

barcode_1.gif
 
Aye, no question, joining a large array would be bad. I was just trying to be clever. ;-)

I agree that erroring out is the way to go... if it works. Have you tested it, by chance?
 
Yep, I threw together a quick test of several differant possibilities:
Code:
<%
Option Explicit

Dim a(), b(20), c(20), d, e, f(), g(), h(), i()

b(0) = ""
e = 0
ReDim f(1)
ReDim g(1)
ReDim Preserve h(1)
ReDim Preserve i(1)
ReDim Preserve i(1)
g(1) = ""

Response.Write "a: Test:" & HasValues(a) & "<br>"
Response.Write "b: Test:" & HasValues(b) & "<br>"
Response.Write "c: Test:" & HasValues(c) & "<br>"
Response.Write "d: Test:" & HasValues(d) & "<br>"
Response.Write "e: Test:" & HasValues(e) & "<br>"
Response.Write "f: Test:" & HasValues(f) & "<br>"
Response.Write "g: Test:" & HasValues(g) & "<br>"
Response.Write "h: Test:" & HasValues(h) & "<br>"
Response.Write "i: Test:" & HasValues(i) & "<br>"
Response.Flush
Response.Write q

Function HasValues(testArray)
	On Error Resume Next
	If IsEmpty(testArray(LBound(testArray))) And IsEmpty(testArray(UBound(testArray))) Then
		HasValues = False
	Else
		HasValues = True
	End If
	On Error Goto 0
End Function
%>
a is an array with no defined bounds
b is an array that has bounds and had a value defined (albeit an empty string, but sitll a value)
c is the same as b without the assignment
d is an Empty variable, ie no assignment
e is a variable assigned 0
f is a ReDimmed array that originally didn't have bounds
g is same as (f) except has had a value assigned to one of it's indexes
h is same as (f) because I was curious if using a Preserve would force the indexes to have avalue
i is same as (h) but with second Preserve, again I was trying to see if it woud force index values to empty strings or 0's
q is a purposeful error to make sure that error checking had resumed to work after exiting the function


The only arrays that return true are (b) and (g), as they are the only ones that a) are arrays, and b)have non-empty contents

Again, it is possible to have an array that you have only assigned values to LBound+1 to UBound-1 which would come back as empty, but I decided that wasa rare enough case that someone would purposely do that, that I could ignore it.

-T

barcode_1.gif
 
*bow* Thanks much, got to make up for my upcoming absence ;)

barcode_1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top