ARRAYS DOUBTS

D

divya

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?
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??

Q3
Can an array hold elements of more than one datatype? If yes how is the
memory allocation done in this case?? are the elements with similar
datatypes stored together??
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??
 
D

Daniel Crichton

divya wrote on 27 Sep 2006 04:08:03 -0700:
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?

Dim arrClashname()
Redim arrClashname(1)
arrClashname(0) = "Start"

Then do the rest as before.

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??

I'd redim and then loop to add the new array items. Not sure if there's an
array joining function built into ASP.
Q3
Can an array hold elements of more than one datatype? If yes how is the
memory allocation done in this case?? are the elements with similar
datatypes stored together??
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??

Each array element is a variant data type - I think each item in memory
includes it's data length.

Dan
 
R

Ray Costanzo [MVP]

divya said:
1.
If The size of the array is unknown during programming time then how is
such array declared??


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???

Have you tried that to see if it produces an error?

Dim arrClashname()
Redim arrClashname(0)
Do While something
If Clash Then
Redim Preserve arrClashname(UBound(arrClassName, 1) + 1)
arrClashname(UBound(arrClashname,1)) = clashname
End If
Loop



Q3
Can an array hold elements of more than one datatype?

Have you tried it?

Dim i
Dim a(2)
a(0) = CLng(393)
a(1) = CByte(1.290812)
a(2) = #2006-09-27#

For i = 0 To UBound(a, 1)
If yes how is the
memory allocation done in this case?? are the elements with similar
datatypes stored together??
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??

You're working with VB Script here, so it's not really the right environment
to work in if you're concerned with such things, I'd say!

Ray at work
 
A

Anthony Jones

divya said:
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.
 
D

divya

Thank you Daniel ,Ray and Anthony for your help and time.
Anthony its really nice of you for giving the three solutions to tackle
the problem with detail explaination and also explaining me in detail
the memory allocation concepts in arrays and during defining and
declaring variables.I was very anxious to know how was it possible for
arrays to hold elements of different datatypes.THanx You.

I just was thinking about a solution without using Redim ,but I don't
know how better it is and what cons it has.It goes as follows:-

Dim AllClashnamesarr
Dim iNumberofclashes : iNumberofclashes = 0
AllClashnamesarr="Empty"

Do while(some condition)
.....
.....(some conditions which may make Clash true)
.....
If (Clash = true) then
inumberofclashes=inumberofclashes+1
if inumberofclashes = 1 then
AllClashnamesarr = clashname
else
AllClashnamesarr =AllClashnamesarr & "." &clashname
End if

Endif
.....
.....
Loop

if not noofclashes=1 then
AllClashnamesarr=Split(AllClashnames,".")
else
AllClashnamesarr=ARRAY(AllClashnamesarr)
End if

For each o in AllClashnamesarr
response.write o
next

I think even here every time we are concatenating clashname to
arrAllClashnames memory allcation and deallocation happens ,its just
that we're not redefining the array again and again.

Thanks a lot again.
Regards
Divya


Anthony said:
divya said:
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.
 
D

divya

Thank you Daniel ,Ray and Anthony for your help and time.
Anthony its really nice of you for giving the three solutions to tackle
the problem with detail explaination and also explaining me in detail
the memory allocation concepts in arrays and during defining and
declaring variables.I was very anxious to know how was it possible for
arrays to hold elements of different datatypes.THanx You.

I just was thinking about a solution without using Redim ,but I don't
know how better it is and what cons it has.It goes as follows:-

Dim AllClashnamesarr
Dim iNumberofclashes : iNumberofclashes = 0
AllClashnamesarr="Empty"

Do while(some condition)
.....
.....(some conditions which may make Clash true)
.....
If (Clash = true) then
inumberofclashes=inumberofclashes+1
if inumberofclashes = 1 then
AllClashnamesarr = clashname
else
AllClashnamesarr =AllClashnamesarr & "." &clashname
End if

Endif
.....
.....
Loop

if not noofclashes=1 then
AllClashnamesarr=Split(AllClashnames,".")
else
AllClashnamesarr=ARRAY(AllClashnamesarr)
End if

For each o in AllClashnamesarr
response.write o
next

I think even here every time we are concatenating clashname to
arrAllClashnames memory allcation and deallocation happens ,its just
that we're not redefining the array again and again.

Thanks a lot again.
Regards
Divya



Anthony said:
divya said:
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.
 
A

Anthony Jones

divya said:
Thank you Daniel ,Ray and Anthony for your help and time.
Anthony its really nice of you for giving the three solutions to tackle
the problem with detail explaination and also explaining me in detail
the memory allocation concepts in arrays and during defining and
declaring variables.I was very anxious to know how was it possible for
arrays to hold elements of different datatypes.THanx You.

I just was thinking about a solution without using Redim ,but I don't
know how better it is and what cons it has.It goes as follows:-

Dim AllClashnamesarr
Dim iNumberofclashes : iNumberofclashes = 0
AllClashnamesarr="Empty"

Do while(some condition)
....
....(some conditions which may make Clash true)
....
If (Clash = true) then
inumberofclashes=inumberofclashes+1
if inumberofclashes = 1 then
AllClashnamesarr = clashname
else
AllClashnamesarr =AllClashnamesarr & "." &clashname
End if

Endif
....
....
Loop

if not noofclashes=1 then
AllClashnamesarr=Split(AllClashnames,".")
else
AllClashnamesarr=ARRAY(AllClashnamesarr)
End if

For each o in AllClashnamesarr
response.write o
next

I think even here every time we are concatenating clashname to
arrAllClashnames memory allcation and deallocation happens ,its just
that we're not redefining the array again and again.

This will perform way worse than even extending an array once per clash. In
this case you will be allocating/deallocating and copying strings which are
likely much larger than 16 bytes.

Even the line:-

AllClashnamesarr =AllClashnamesarr & "." &clashname

Will allocate a string to perform the first & and the allocate yet another
string to perform the other & then deallocate the first allocation and
deallocate the string currently held by AllClashnamesarr before assigning to
it.

I suspect a hybrid approach would best suit your needs.

Thanks a lot again.
Regards
Divya


Anthony said:
divya said:
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.
 
D

divya

Hi whatz the difference in defining the array as
Dim arr() and Dim arr(6) and the memory allocation. Dim arr(6)
immediately allocates memory for 7 elements each 16 bytes what about
Dim arr()?

I tried a small code

dim c
c=10
Dim arr(c)
Gives error :-
Microsoft VBScript compilation (0x800A0402)
Expected integer constant
I doesnot allow to give a variable as size of the array.

similarly
c=10
dim arr()
for i = 0 to c
arr(i)=i
next

gives Subscript out of range error.

but when I give
Dim arr()
c=10
Redim arr(c) 'For redim it takes variables
for i = 0 to c
arr(i)=i
next

This works fine.

Why is it that it gives the errors?






Anthony said:
divya said:
Thank you Daniel ,Ray and Anthony for your help and time.
Anthony its really nice of you for giving the three solutions to tackle
the problem with detail explaination and also explaining me in detail
the memory allocation concepts in arrays and during defining and
declaring variables.I was very anxious to know how was it possible for
arrays to hold elements of different datatypes.THanx You.

I just was thinking about a solution without using Redim ,but I don't
know how better it is and what cons it has.It goes as follows:-

Dim AllClashnamesarr
Dim iNumberofclashes : iNumberofclashes = 0
AllClashnamesarr="Empty"

Do while(some condition)
....
....(some conditions which may make Clash true)
....
If (Clash = true) then
inumberofclashes=inumberofclashes+1
if inumberofclashes = 1 then
AllClashnamesarr = clashname
else
AllClashnamesarr =AllClashnamesarr & "." &clashname
End if

Endif
....
....
Loop

if not noofclashes=1 then
AllClashnamesarr=Split(AllClashnames,".")
else
AllClashnamesarr=ARRAY(AllClashnamesarr)
End if

For each o in AllClashnamesarr
response.write o
next

I think even here every time we are concatenating clashname to
arrAllClashnames memory allcation and deallocation happens ,its just
that we're not redefining the array again and again.

This will perform way worse than even extending an array once per clash. In
this case you will be allocating/deallocating and copying strings which are
likely much larger than 16 bytes.

Even the line:-

AllClashnamesarr =AllClashnamesarr & "." &clashname

Will allocate a string to perform the first & and the allocate yet another
string to perform the other & then deallocate the first allocation and
deallocate the string currently held by AllClashnamesarr before assigning to
it.

I suspect a hybrid approach would best suit your needs.

Thanks a lot again.
Regards
Divya


Anthony said:
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.
 
B

Bob Barrows [MVP]

divya said:
Hi whatz the difference in defining the array as
Dim arr() and Dim arr(6) and the memory allocation. Dim arr(6)
immediately allocates memory for 7 elements each 16 bytes what about
Dim arr()?

This is covered in the documentation
(http://www.microsoft.com/downloads/details.aspx?FamilyID=01592c48-207d-
4be1-8a76-1c4099d7bbb9&DisplayLang=en)

Dim arr(6)
creates a static array which cannot be redimmed. Yes, the space is
immediately created.

Dim arr()
creates a variable-length array

Given the comments already made by the other respondents in this thread,
why are you still worried about memory allocation?

Anyways, you may want to browse through Eric Lippert's blog (he was
responsible for much of what we see in vbscript today):
http://blogs.msdn.com/ericlippert/default.aspx
 
D

divya

I am really sorry for bugging you all .
But
Dim arr()
arr=ARRAY("5","6","7")

this is wrong and gives error.
My understanding
The array() function creates a dynamic array which is then assigned to
a
variable which has to be a Variant. arr() is not a variant because its
size cannot be incresed directly .It has to be done using REDIM.

I am slightly confused with when to declare array as Dim arr , Dim
arr() and Dim arr(5).
I know when using ARRAY function use Dim arr , when the size is fixed
during programming
then use Dim arr(6) and when size is not fixed and has to change during
runtime use
Dim arr(). But why is still not clear.

Thnks
Divya
 
B

Bob Barrows [MVP]

divya said:
I am really sorry for bugging you all .
But
Dim arr()
arr=ARRAY("5","6","7")

this is wrong and gives error.

This is correct. It should give an error.
My understanding
The array() function creates a dynamic array which is then assigned to
a
variable which has to be a Variant. arr() is not a variant because
its size cannot be incresed directly .It has to be done using REDIM.

I am slightly confused with when to declare array as Dim arr , Dim
arr() and Dim arr(5).

You're confused? Why? You just explained it perfectly.
I know when using ARRAY function use Dim arr , when the size is fixed
during programming
then use Dim arr(6) and when size is not fixed and has to change
during runtime use
Dim arr(). But why is still not clear.

Why does the "why" need to be clear? It's the result that's important, and
you've just explained the result perfectly. However, if you wish to pursue
this, then send an email to Eric Lippert. He's got a contact page at his
blog: msgbox join(arr)
http://blogs.msdn.com/ericlippert/
 
A

Anthony Jones

divya said:
I am really sorry for bugging you all .
But
Dim arr()
arr=ARRAY("5","6","7")

this is wrong and gives error.
My understanding
The array() function creates a dynamic array which is then assigned to
a
variable which has to be a Variant. arr() is not a variant because its
size cannot be incresed directly .It has to be done using REDIM.

I am slightly confused with when to declare array as Dim arr , Dim
arr() and Dim arr(5).
I know when using ARRAY function use Dim arr , when the size is fixed
during programming
then use Dim arr(6) and when size is not fixed and has to change during
runtime use
Dim arr(). But why is still not clear.

The why of this behaviour is answered by the history and background of
VBScript.

Whilst VBScript is an 'interpreted' language (note use of such terms are
fairly loose these days) much of it's codebase has been borrowed indeed
shared with VB (a 'compilable' language). Using fixed arrays in VB allowed
for certain compilier optimisations not relevant in VBScript but I guess
some syntax rules have folllowed.

The return value of an Array() function is in fact a variant. In addition
to all the other data types a variant can hold it can also hold an array.
It is often said that VBScript has only one data type, the variant, but in
fact it has two the other being the array. You can not assign a variant to
an array however you can assign an array to a variant.

If you are going to define an array then use ReDim to dimension it then you
can actually use variant:-

Dim vnt

ReDim vnt(4)

Creates an array and stores it in a variant variable.

Using a variant to hold an array is slightly slower since VBScript first has
to determine that the variant is an array then dereference the stored
pointer to the array before doing what it would normally do against an array
variable. However as you have seen it is more flexible. It is the only way
to get the array from the Array function and any results from functions you
might want to create that return an array would also need to be stored in a
variant (VBScript functions return only variant).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top