[ASP] Arrays - removing an item?

R

Rob Meade

Hi all,

I have an array of lets say 10 items, I now want to remove an item, lets say
from somewhere in the middle, based on one of the element values equalling a
value I specify - is there a "clever" and "quick" way of doing this or, will
I need to iterate through it looking for a match, and build another array
where I dont find the match etc...

Any advice appreciated..

Regards

Rob
 
B

Bob Barrows [MVP]

Rob said:
Hi all,

I have an array of lets say 10 items, I now want to remove an item,
lets say from somewhere in the middle, based on one of the element
values equalling a value I specify - is there a "clever" and "quick"
way of doing this
nope

or, will I need to iterate through it looking for a
match, and build another array where I dont find the match etc...
yup
 
R

Rob Meade

...

LOL..

Thanks Bob - I've been doing this for 2 hours now and its driving me bl**dy
crazy....I've hitting some response writes to prove I'm in a section of code
where it should be redim'ing preserving the temp array and resizing it
etc....it doesn't seem to be growing, so I changed the increment to 10 and
still it comes out with a response.write UBound(myArray, 2) as 1 - I mean -
WHAT THE HELL!!!

arrghhhh....

:eek:(
 
B

Bob Barrows [MVP]

Rob said:
...


LOL..

Thanks Bob - I've been doing this for 2 hours now and its driving me
bl**dy crazy....I've hitting some response writes to prove I'm in a
section of code where it should be redim'ing preserving the temp
array and resizing it etc....it doesn't seem to be growing, so I
changed the increment to 10 and still it comes out with a
response.write UBound(myArray, 2) as 1 - I mean - WHAT THE HELL!!!

arrghhhh....
Are you using a dynamic array? Do you know how many items you'll be
removing? if so, why not initially redim the array to the intended size?
repeated "redim preserve"s is not very efficient. Actually, I've just
had a brainstorm for a quicker way to do it. Here is some code
illustrating both ways to remove a single item from an array:
<%
option explicit
dim ar
ar=array("a","b","c")
response.write join(ar,", ") & "<BR>"
ar=remove(ar,"b")
response.write join(ar,", ")
ar=array("a","b","c")
response.write "<BR>" & join(ar,", ") & "<BR>"
ar=remove2(ar,"c")
response.write join(ar,", ")
'-------------------------------------------
function remove(byval arsrc,item)
dim ardest(), i, j, curitem
redim ardest(ubound(arsrc)-1)
j=0
for i = 0 to ubound(arsrc)
curitem=arsrc(i)
if curitem<>item then
ardest(j) = curitem
j=j+1
end if
next
remove=ardest
end function

'-------------------------------------------
function remove2(byval arsrc,item)
dim s, ardest
s=join(arsrc,"|") & "|"
s=replace(s,item & "|","")
ardest=split(left(s,len(s)-1),"|")
remove2=ardest
end function
%>
 
E

Evertjan.

Rob Meade wrote on 12 jun 2006 in
microsoft.public.inetserver.asp.general:
Hi all,

I have an array of lets say 10 items, I now want to remove an item,
lets say from somewhere in the middle, based on one of the element
values equalling a value I specify - is there a "clever" and "quick"
way of doing this or, will I need to iterate through it looking for a
match, and build another array where I dont find the match etc...

Any advice appreciated..

<script language='jscript' runat='server'>

a = ["a","b","c"]

a[1] = null

a = a.join('-').replace(/\-\-/g,'-').split('-')

alert(a)


</script>
 
E

Evertjan.

Evertjan. wrote on 13 jun 2006 in microsoft.public.inetserver.asp.general:
Rob Meade wrote on 12 jun 2006 in
microsoft.public.inetserver.asp.general:
Hi all,

I have an array of lets say 10 items, I now want to remove an item,
lets say from somewhere in the middle, based on one of the element
values equalling a value I specify - is there a "clever" and "quick"
way of doing this or, will I need to iterate through it looking for a
match, and build another array where I dont find the match etc...

Any advice appreciated..

<script language='jscript' runat='server'>

a = ["a","b","c"]

a[1] = null

a = a.join('-').replace(/\-\-/g,'-').split('-')

alert(a)

I mean:

response.write(a.split(','));
</script>

If your Q was about ASP-vbscript only please specify.
 
E

Egbert Nierop \(MVP for IIS\)

Rob Meade said:
Hi all,

I have an array of lets say 10 items, I now want to remove an item, lets
say from somewhere in the middle, based on one of the element values
equalling a value I specify - is there a "clever" and "quick" way of doing
this or, will I need to iterate through it looking for a match, and build
another array where I dont find the match etc...

Any advice appreciated..

Why dont you just use the Dictionary object unless the array needs to be
stored in the session, then you'd better not do that.

(Never store objects in the session in classic ASP)
 
R

Rob Meade

...
Why dont you just use the Dictionary object unless the array needs to be
stored in the session, then you'd better not do that.

Hi Egbert,

Thanks for the reply - alas I am using Sessions as I need to store the
basket contents for an eCommerce solution.

Regards

Rob
 
R

Rob Meade

...

[snip example]

Hi Bob,

Thanks for the reply - and example - I managed to get it working but I will
give your example a whirl shortly as I suspect its a bit better than mine
:eek:)

Regards

Rob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top