I'm creating a dynamic favourites list using arrays.
So far all I wanted to do was to make sure I can create the array, add and item, check the size with ubound and remove an item, writing this to the page as I go to make sure it works.
It all works fine in a normal array but once I've created a multi dimensional array the ubound no longer works and anything I do based on the ubound value fails after that.
Bear in mind that I wrote the syntax by making it up as I went along using code bits I've found and articles I read and what knowledge I have of VB Script. So it's not necessarily optimal. That being disclaimed here is the code:
<% Dim myArray() 'Create the dyamic array
ReDim myArray(1,4) 'give it a size with redim
'fill the array with items
myArray(0,0) = "Skye"
myArray(1,0) = "12 months"
myArray(0,1) = "Sto"
myArray(1,1) = "40 years"
myArray(0,2) = "Ilana"
myArray(1,2) = "38 years"
myArray(0,3) = "Jamie"
myArray(1,3) = "13 years"
myArray(0,4) = "Ribi"
myArray(1,4) = "3 years"
%>
<strong>The first array, written to the page</strong><br>
<% 'write the array items to the page
For i = 0 to 4
response.write(myArray(0,i
))&"
"&(myArray
(1,i)& "<br>")
Next %>
<br> <% 'find the size of the array and write that to the page %>
<strong>The size of the first array is <% = UBound(myArray) -UBound(myArray) - LBound(myArray) %></strong><br><br>
<% Dim maxNum, newMaxNum 'create a current size variable and set it equal the array size and create a new max size variable
maxNum = UBound(myArray) - LBound(myArray)
newMaxNum = MaxNum + 1 'grow it by one place using the current size + 1%>
<% ReDim Preserve myArray(1,newMaxNum) 'redim and preserve the array %>
<% myArray(0,newMaxNum) = "Heanly"
myArray(1,newMaxNum) = "3 years"'Give the new item a name%>
<% 'write the new array to the page
For i = 0 to newMaxNum
response.write(myArray(0,i
))&"
"&(myArray
(1,i) & "<br>")
Next %>
<br><strong>The size of the new array is <% = UBound(myArray) - LBound(myArray) %> </strong><br><br>
<strong> Remove an item from the Array</strong><br>
<%
removedItem = False
Dim itemToRemove
itemToRemove = "Sto"
arrayLength = UBound(myArray)
for i = 0 to arrayLength
if (myArray(1,i) = itemToRemove) then
' If we found our search term in array
' we are copying array to a new array list
myArray(i) = myArray(arrayLength)
' So now our first value must be TRUE
removedItem = True
end if
next
if (removedItem = True) then
' Let's make a new array and pass all values
redim preserve myArray(arrayLength - 1)
end if %>
<% newMaxNo = UBound(myArray) - LBound(myArray) %>
<% 'write the array sans removed item to page %>
<br>
<% For i = 0 to newMaxNo %>
<% response.write(myArray(0,i
))&"
"&(myArray
(1,i)) %><br>
<% Next %>
<br><br>
<strong>Add yet another item to the array</strong><br><br>
<%maxNum = UBound(myArray) - LBound(myArray)
newMaxNum = MaxNum + 1 'grow it by one place using the current size + 1%>
<% ReDim Preserve myArray(1,newMaxNum) 'redim and preserve the array %>
<% myArray(0,newMaxNum) = "Moss"
myArray(1,newMaxNum) = "10 years"'Give the new item a name%>
<% 'write the new array to the page
For i = 0 to newMaxNum
response.write(myArray(0,i
))&"
"&(myArray
(1,i) & "<br>")
Next %>
Start Free Trial