473,395 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

ARRAYS DOUBTS

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

Sep 27 '06 #1
12 1783
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
Sep 27 '06 #2

"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??
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)
Response.Write a(i) & "<hr />"
Next
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
Sep 27 '06 #3

"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.
>

Sep 27 '06 #4
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 Jones wrote:
"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.
Sep 28 '06 #5
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 Jones wrote:
"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.
Sep 28 '06 #6

"divya" <di*********@yahoo.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
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 Jones wrote:
"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.
>

Sep 28 '06 #7

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 Jones wrote:
"divya" <di*********@yahoo.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
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 Jones wrote:
"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.
>
Sep 29 '06 #8
divya wrote:
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/d...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
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Sep 29 '06 #9
Bob Barrows [MVP] wrote:
Dim arr()
creates a variable-length array
I should have said "dynamic array"
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Sep 29 '06 #10
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

Bob Barrows [MVP] wrote:
Bob Barrows [MVP] wrote:
Dim arr()
creates a variable-length array

I should have said "dynamic array"
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Oct 3 '06 #11
divya wrote:
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/

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Oct 3 '06 #12

"divya" <di*********@yahoo.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
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).

Thnks
Divya

Bob Barrows [MVP] wrote:
Bob Barrows [MVP] wrote:
Dim arr()
creates a variable-length array
I should have said "dynamic array"
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.

Oct 3 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: franky.backeljauw | last post by:
Hello, following my question on "std::copy versus pointer copy versus member copy", I had some doubts on the function memcpy, as was used by tom_usenet in his reply. - Is this a c++ standard...
0
by: abbas reji | last post by:
--0-599929911-1059996886=:4358 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Content-Id: Content-Disposition: inline ...
197
by: Steve Kobes | last post by:
Is this legal? Must it print 4? int a = {{1, 2}, {3, 4}}, *b = a; printf("%d\n", *(b + 3)); --Steve
43
by: Anitha | last post by:
Hi I observed something while coding the other day: if I declare a character array as char s, and try to use it as any other character array..it works perfectly fine most of the times. It...
6
by: ritesh | last post by:
Hi, I have been reading some text on C and C++ (i.e advanced books). One of the books mentioned that C++ requires a runtime support whereas C does not - what the author was trying to say was...
17
by: Paminu | last post by:
Is there no such a thing as an ArrayOutOfBounds exception in C? I just did: int main() { int a; // a is an array that contains 4 integer. a = 999; // Thought this would give an error!...
5
by: Dennis | last post by:
I wish to build up a byte array from various sources of different lengths. I can either use a byte array and redimension it as needed as I add the various values or use an arraylist which...
6
by: Michael Gray | last post by:
VS 2003 VB.net Win2000 SP4 The System.Array class seems to be limited to 32 bit addresses, meaning that one can only assign 2^32 elements. Is there any way that I can have an array that...
2
by: ucfcpegirl06 | last post by:
Hello, I have a file that I am reading date from. File format is as follows: a 556644f3 b 44545351 ......
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.