Click to See Complete Forum and Search --> : Search and Delete an array element in ASP


rskshiva
11-18-2004, 04:36 AM
Dear All,

I will be happy if any one will provide the solution to search and delete an array element using ASP.
I had wrote the code as,

For i = 0 to UBound(arrMail)
if ( arrMail(i) = "suresh@angleritech.com") then
Response.Write i & ".) " & arrMail(i) & "<br>"
arrMail.Delete i
end if
Next

But it is not working and displays an error as follows.
---------
Microsoft VBScript runtime error '800a01a8'
Object required
---------

Pls mail me: siva@angleritech.com
:confused:

russell
11-18-2004, 11:54 AM
There is no delete() method for vbScript arrays. No reason we can't write one though:


Sub delete(ByRef ar, ByVal idx)
Dim i
Dim ub

ub = UBound(ar) - 1

For i = idx To ub
ar(i) = ar(i + 1)
Next

ReDim Preserve ar(ub)

End Sub

We can try it out like this:

<%
Dim ar
Dim i

ar = Array(1,2,3)

Response.Write "Original Array:<br>" & vbCrLf
For i = 0 to ubound(ar)
Response.Write ar(i) & "<br>" & vbCrLf
Next

Response.Write "<hr>" & vbCrLf

Response.Write "Original Array After Delete:<br>" & vbCrLf
delete ar, 1 '' pass the name of the array, and the index of the element to delete

For i = 0 to ubound(ar)
Response.Write ar(i) & "<br>" & vbCrLf
Next

Sub delete(ByRef ar, ByVal idx)
Dim i
Dim ub

ub = UBound(ar) - 1

For i = idx To ub
ar(i) = ar(i + 1)
Next

ReDim Preserve ar(ub)

End Sub
%>

rskshiva
11-18-2004, 11:15 PM
Thanks a lot russell. Now i am able to delete the particular item.
But i need to delete the duplicate items also.
I am not able to decrease the size of an array.

The array is created dynamically and having email ids, If any duplicate ids found i need to delete those items from the array.

Kindly help me.

Mail me: siva@angleritech.com

russell
11-18-2004, 11:19 PM
is it a two-dimensional array, perhaps returned from a database with a GetRows function?

rskshiva
11-19-2004, 12:31 AM
No two dimensional array.

See the code below.

<%
Dim arrMail,i
arrMail=Array("a@x.com","b@x.com","c@x.com","d@x.com","e@x.com","f@x.com","g@x.com","b@x.com","h@x.com","i@x.com","j@x.com")

For i = LBound(arrMail) to UBound(arrMail)
if ( arrMail(i) = "suresh@angleritech.com") then
delete arrMail, i
ReDim Preserve arrMail(UBound(arrMail)-1)
end if
Next

Sub delete(ByRef ar, ByVal idx)
Dim i
Dim ub

ub = UBound(ar) - 1

For i = idx To ub
ar(i) = ar(i + 1)
Next
ar(ub+1)=""
ReDim Preserve ar(ub+1)
End Sub
%>
It displays an error message as below.
--------------
Microsoft VBScript runtime error '800a0009'
Subscript out of range: '9'
/arraydel.asp, line 6
--------------
In the above array, b@xcom is present 2 times. If any duplicates found, i need to delete those items. As of now when i run the above code, the last item "j@x.com" moved to previous index. But the origional place also get retained in the array. I am not able to redim the array and delete the duplicate items.

Kindly suggest me.

Regards, Siva (siva@angleritech.com)

russell
11-19-2004, 11:47 AM
are the email addresses and ids coming from a database? eaiser to de-dupe on the db.

anyway,

take the redim preserve out of this part (the delete method already does that):

For i = LBound(arrMail) to UBound(arrMail)
if ( arrMail(i) = "suresh@angleritech.com") then
delete arrMail, i
ReDim Preserve arrMail(UBound(arrMail)-1)
end if
Next

Finally, you are shortening the array, so you are guranteed to go out of bounds if you delete any items the way your loop is set up. Because the ubound doesn't change in the FOR statement. So, try changing the above snippet to:

For i = 0 to UBound(arrMail)
If i > UBound(arrMail) Then Exit For

if (arrMail(i) = "suresh@angleritech.com") then
delete arrMail, i
end if
Next

rskshiva
11-20-2004, 04:45 AM
Dear Russell,

Thanks a lot.

Best Regards,
Siva R