You may notice that in the array declaration step we set 2 between the array braces, while in the array filling we have three elements, the point is 2 is equals to the maximum index and not to size, for example : arrNew(3) means size is equal 4. A question may come to the mind, what will happen if we set larger than the limit? The answer that you will error message telling you that you have declared arrColors with fixed number X while you are trying to index at Y. The solution to this problem is using dynamic arrays instead of fixed ones. To declare dynamic array you just follow the example below:
<% X = 15 Dim arrColors () ReDim arrColors (X) %>
Creating a multi-dimensional array
<% X = 15 Y = 10 Dim arrColors () ReDim arrColors (X,Y) %>
Now we control the size of the array dynamically with a variable, on your running code you can change the array size using a variable, another case, is when you want to increase the size of array while keeping the existing array values if exists, you will use the Preserve keyword :
<% arrColors (0)="Red" arrColors (1)="Green" Keep Added two elements and re-declare and add two more elements ReDim Preserve arrColors (3) arrColors (2)="Blue" arrColors (3)="Yellow" %>
Using Preserve keyword in Loop,
<% Dim arrColors () for i = 1 to 10 ReDim Preserve arrColors (i) arrColors(i)="Color"& i next %>
To print the content of array use:
<% For Each myColor In arrColors Response.Write(myColor &"<br>") Next %>
To get the current length of an array you can use the function UBound(), as a index starts at 0 and if there 7 elements in the array the UBound function will return 8, so the size is equals UBound Return plus one :
for i=0 to uBound(arrColors) Response.Write "<br>"& arrColors(i) Next
One of important functionality exists in ASP(VB) arrays is splitting, here we split strings using the function split(), here we split strings using a delimiter that is can be space, comma , or any character you define.
mArray=split("Name is samy"," ") Space is a delimiter
and this is equals to
mArray(0)="Name" mArray(1)="is" mArray(2)="samy"
Note that you also define the maximum splits used the third parameter in the split function like this :
mArray=split("Name is samy and i am a developer"," ",3)
and this is equals to :
mArray(0)="Name" mArray(1)="is" mArray(2)="samy and i am a developer"
as we split the strings we can also concatenate strings using the join() function, see the example below :
Last function is also a ASP(VP) function which is filter() function , this function used for strings search, the function prototype is as follows :
Filter(strArray, Target, include, comparetype)
strArray is the array where will be searching in , Target is the string looking for . include, comparetype parameters are optional. include can be True or False. If it is True then all matching elements of the strArray is returned The parameter comparetype is also a Boolean , if it is set to 0 then it is a binary comparison so it is case sensitive , 1 ignores case sensitive.