"divya" <di*********@yahoo.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
1.
If The size of the array is unknown during programming time then how is
such array declared??
Suppose there is an Array named arrClashname(size unknown at start),
Now when the clash occurs I want to store that name into the
arrClashname,
Therez a do while loop inside which has some conditions which decied
when clash occurs and a variable named Clash is set to true and its
name has to be added to arrClashtime.I also have a variable which hold
number of clashes (inumberclashes)which gets incremented evry time
clash occurs.
Since I don't know how many clashes may occur inside the loop I cannot
define a static arrClashname.
This way can it be done:-
Dim arrClashname(0)
arrClashname(0)="Start"
Do while(some condition)
....
....(some conditions which may make Clash true)
....
If (Clash = true) then
inumberofclashes=inumberofclashes+1
ReDim Preserve arrClashname(inumberofclashes)
arrClashes(inumberofclashes) = clashname
Endif
....
....
Loop
Is this the right way of doing??? Or there is some other way?
There are three approaches to this problem each having pros and cons:-
i)
Dim arr()
Dim count : count = 0
For i = 0 to x
If condition Then
ReDim Preserve arr(count)
arr(count) = fn(x)
count = count + 1
End If
Next
Advantage this has is its very simple.
Problems with this approach is its not guaranteed that the memory allocation
needed to extend the the array each time (which needs to be in contiguous
memory) can be generated by extending the existing allocation (in fact it
may not even be an optimisation used internally), this could result in many
allocations/deallocations of memory as the array gets extended. The more
often condition is true the worse this problem gets. You'd also need to be
careful with code that uses the resulting arr in case it never got created
(because condition was never true) this is mitigated by being able to test
for count = 0.
ii)
Dim arr()
Dim count : count = 0
ReDim arr(x)
For i = 0 to x
If condition Then
arr(count) = fn(x)
count = count + 1
End If
Next
If count 0 then
ReDim Preserve arr(count-1)
Else
Erase arr
End If
This approach doesn't suffer at all with multiple memory
allocation/deallocation issues. If count is significantly less than x then
we initially allocated a larger array than we need. As count approaches x
this approach is very effecient. One variation might be not to bother with
the truncatiing Redim at the end and just use count - 1 as the upper bound
when accessing the array.
iii)
Const chunkSize = 100
Dim arr()
Dim count : count = 0
ReDim arr(chunkSize)
For i = 0 to x
If condition Then
If count UBound(arr) ReDim Preserve arr(count + chunkSize)
arr(count) = fn(x)
count = count + 1
End If
Next
If Count 0 Then
ReDim Preserve arr(count-1)
Else
Erase arr
End If
This approach is a hybrid of the two above. The number of potential memory
allocations/deallocations are limited to 100th of what they are in the first
approach yet the starting size of the resulting array is small. A variation
that I prefer when hiding this cort of stuff in a class is to start with a
16 element array and double it each time it needs to be extended.
One other modification I use is to always have an element 0 which remains
unused. The first real element is at 1. This simplfies code somewhat (no
need to worry the UBound(arr) will error not need for the final If ... Erase
business above).
Q2
How can I add more elements to an array created using ARRAY()
function??
Suppose
Dim arrhit
arrhit=ARRAY("start","dfd")
Now how can I add more elements to this array??
arrhit[1]="ssdfdf" directly or use redim first to increase the size and
then add new data??
It's still just an array and can be the subject of a Redim Preserve
statement.
Q3
Can an array hold elements of more than one datatype?
No ;) There is only one datatype. Variant. Variant though can hold many
different datatypes.
>If yes how is the
memory allocation done in this case??
All elements in an Array in VBScript are 16 Bytes the size of a variant.
>are the elements with similar datatypes stored together??
All the elements of an array are allocated in a single contiguous block of
memory. For many fixed length data types such as Long, boolean, currency
etc. their values are stored in the variant itself. Strings on the other
hand are allocated in separate memory with pointers to the strings being
stored in the variant. There is no corelation between a string elements
position in an array and the actual memory location of the string itself.
suppose array of integers so now if array starts at 1000 since integers
so second element will be at location 1002 third at 1004 and so easy to
fetch.But if it can hold elements of any datatype how does it know that
how many bytes should be fetched for a element which can be of any
datatype??
All elements are 16 bytes there is no memory advantage to store integers
instead of longs or even currency (an 8 byte datatype) since the variant
structure accommadates them.
>