Re: A test for an Array

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



hi George,

I think you've got some good answers about the need for error handling etc. In VB6 you could do it differently as you could use VarPtr and actually test the structure in memory.

One thing you might want to look at is this part of your code :

<Script language=vbscript>
Function MyFilter(), i
Dim tmpArray()
i = 0
'tmpArray is populated
For Each x in tmpArray
ReDim v(i)
v(i) = x
i = i + 1
Next
Emd Fimction
</Script>

You shouldn't be using ReDim inside a loop, and especially not like that as you are loosing the data that should be in the earlier elements. I'd suggest first dimensioning v to the same bounds as tmpArray, or alternatively using ReDim preserve, but realize that ReDim preserve is inefficient, causing the array to be copied each time.






"George Hester" <hesterloli@xxxxxxxxxxx> wrote in message news:ORBWOBeuIHA.552@xxxxxxxxxxxxxxxxxxxxxxx
I make a dynamic array v something like this:

<Script language=vbscript>
Dim v()
Dim iCnt
iCnt = 0
</Script>

<Script language=vbscript>
Function MyFilter(), i
Dim tmpArray()
i = 0
'tmpArray is populated
For Each x in tmpArray
ReDim v(i)
v(i) = x
i = i + 1
Next
Emd Fimction
</Script>

<Script language=vbscript event=onmousedown for=idT>
Dim vBound, w()
'Problem here
vBound = UBound(v)
If Err.Number = CLng(9)
Err.Clear
Else
If iCnt < vBound Then
'Do stuff
iCnt = iCnt + 1
Else
'Problem here
ReDim v(0)
iCnt = 0
End If
End If
</Script>

There are two problems here and I am hoping someone can show me a better
way. The vBound = UBound(v) will give an error "Subscript out of range" if v
is an "empty" array. So I have checked whether an error has occurred to tell
me that. But I do not like throwing an Error if I can avoid it. Is tthere a
better way to test for an empty array? Not just empty elements because in
fact there are no elements yet. This test is the best I can come up with so
far.

And for the second problem. I do want v to be an empty array most of the
time. Trouble is ReDim v(0) is not an empty array. The first element is
empty but that isn't what I need. I need the array itself to be empty; no
dimensions yet. That is why I have w() Dimed. Because if I could set v = w
then it would be an empty array. But I cna't do that. I get a script error.
It works wuth scaler values but not arrays. So is there some way to make an
array EMPTY again, not just containing empty values? Set v = Empty didn't
work. In other words IsEmpty(v) is still false.

Thanks for any insight here.

--

George Hester
_________________________________



.



Relevant Pages

  • Re: A test for an Array
    ... from the code I couldn't tell tmpArray was a copy as it only says 'tmpArray is populated ... in the line before the loop, so I assumed you omitted that code; as it is, the for loop would never execute. ... As to ReDim, each time you call it, a complete new buffer the size of the array has to be allocated. ... > is an "empty" array. ...
    (microsoft.public.vb.general.discussion)
  • Re: A test for an Array
    ... It is interesting that you zoomed in on tmpArray. ... Dim iCnt ... is an "empty" array. ...
    (microsoft.public.vb.general.discussion)
  • Re: array question
    ... I've always been baffled why MS never included any means to see if an array ... is empty without the risk of raising an error. ... Dim Items() As String ... Redim Items ...
    (microsoft.public.vb.general.discussion)
  • Re: How to copy non-contiguous columns to a text file
    ... a single output string could theoretically exceed the limits of the String data type. ... Unlikely as all that might seem, I refer to design algorithms that do not break when expected limits are exceeded. ... I vaguely remember that some other applications requires the empty line at the end when they import the file. ... The last line, then, should be the Record.Count since the first record is elementof the array. ...
    (microsoft.public.excel)
  • Re: Macros in php
    ... From the PHP manual for empty: ... "The following things are considered to be empty: ... array() ... practice in PHP. ...
    (comp.lang.php)