Amit Shah
Amit Shah

Reputation: 35

Dynamically reduce the array size in classic ASP

I have an array. I loop through it and display the value. Once displayed, I want to remove that particular node so that the array size reduces.

Example

tot = 20
redim values(tot)
for i=1 to 20
  values(i) = i
next

for i=1 to ubound(values)
  if values(i) = 10 then
    ' i do my work here.
    ' After my work is done, i want to remove the node values(10) 
    ' so that the ubound of my array changes to 19 and not 20 
    ' when i loop through next time.
  end if
next

Please help.

Upvotes: 0

Views: 2003

Answers (1)

Flakes
Flakes

Reputation: 2442

You can change the size of your array and keep the values using Redim Preserve

To remove a node, you could try this:

tot = 19
redim values(tot)

for i=0 to UBound(values)
  values(i) = i+1
next

Response.write  "Initial Size:"& UBound(values) & "<br/>"

bMoveUp = false

for i=0 to ubound(values)


   if values(i) = 10 then
    'do your thing with the i=10 the element

    bMoveUp = true
   end if

    if bMoveUp = true Then
    if i <> ubound(values)  then
        values(i) = values(i+1)
    end if
    End If


next

Redim Preserve Values(ubound(values)-1)

Response.write "Final Size:"& UBound(values) & "<br/>"


for i=0 to UBound(Values)
  Response.write values(i) &  "<br/>"
next

Upvotes: 1

Related Questions