6

Given the following array as an example...

arr(0)(0) = 3
arr(0)(1) = name
arr(0)(2) = address
arr(1)(0) = 7
arr(1)(1) = name
arr(1)(2) = address
arr(2)(0) = 14
arr(2)(1) = name
arr(2)(2) = address

I need to delete the middle element (id=7) from the array. I understand that I need to loop through the array and move each record that isnt to be deleted into a new array. I tried like this...

Dim newArr,i
Redim newArr(Ubound(arr))

For i = 0 to Ubound(arr)
    If (CStr(arr(i)(0)) <> 7 ) Then
        newArr(i) = arr(i)
    End if
Next

When debugging this I can see the if statement work so I know only 2 elements are copied but newArr is empty at the end of this. What am I missing. I am a PHP coder that is new to classic asp and Im used to having array functions that make this kind of thing unnecessary. Any help appreciated. Thank you.

Kul-Tigin
  • 16,728
  • 1
  • 35
  • 64
alan shaw
  • 147
  • 1
  • 3
  • 8
  • I am aware of scripting.dictionary, however, the array in question is used throughout a very large site and I do not have access to alter it at this point. – alan shaw Jul 19 '13 at 19:11
  • 2
    Not really a solution but: this is not an asp-problem. ASP is just a container for the scripting language, in your case VBscript. Your problem is therefore a VBscript problem. This knowledge may help you search google more successfully. – Jeff Jul 19 '13 at 20:30

5 Answers5

7

Instead of using array you can give Scripting.Dictionary a try.

It is much more flexible, and has, among others Remove method.

Yuriy Galanter
  • 38,833
  • 15
  • 69
  • 136
  • Unfortunately I dont have access to the creation of the array. It is a very large site and the array in question is used throughout. Not possible at this point to get away from it. – alan shaw Jul 19 '13 at 19:09
7

You don't need new array, you can just reassign the items and "crop" the array:

Const removalIndex = 1
For x=removalIndex To UBound(arr)-1
    arr(x) = arr(x + 1)
Next
ReDim Preserve arr(UBound(arr) - 1)

This code will remove the array item at index 1 from the main array. If you don't know in advance the index of the item to remove, you can easily find it with a simple loop over the array.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
5

I suggest using Scripting.Dictionary and using it as a List/collection instead, as it allows for insertions and deletions. See here: Lists in VBScript

Community
  • 1
  • 1
Dai
  • 141,631
  • 28
  • 261
  • 374
0

I don't know the definitive answer, but if I were to take a stab in the dark guess I'd suggest that since the array is two dimensional maybe you have to explicitly refer to it that way?

Dim newArr,i
Redim newArr(Ubound(arr),3)

For i = 0 to Ubound(arr)
    If (CStr(arr(i)(0)) <> 7 ) Then
        newArr(i)(0) = arr(i)(0)
        newArr(i)(1) = arr(i)(1)
        newArr(i)(2) = arr(i)(2)
    End if
Next
wweicker
  • 4,833
  • 5
  • 35
  • 60
0

I see some VBScript syntax issues. First:

arr(0)(0) = 3  'ERROR: Subscript out of range
arr(0, 0) = 3  'CORRECT

Next:

ReDim newArr(Ubound(arr))  'this is 1 dimensional array
newArr(0) = arr(0)         'this will NOT work
newArr(0) = arr(0, 0)      'this will work

And finally: why you convert to String and then compare it to an Integer with:

(CStr(arr(i)(0)) <> 7)
Panayot Karabakalov
  • 3,109
  • 3
  • 19
  • 28