Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > [ASP] Arrays - removing an item?

Reply
Thread Tools

[ASP] Arrays - removing an item?

 
 
Rob Meade
Guest
Posts: n/a
 
      06-12-2006
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


 
Reply With Quote
 
 
 
 
Bob Barrows [MVP]
Guest
Posts: n/a
 
      06-12-2006
Rob Meade wrote:
> 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

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.


 
Reply With Quote
 
 
 
 
Rob Meade
Guest
Posts: n/a
 
      06-12-2006
"Bob Barrows [MVP]" wrote ...

> nope
> yup


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....

(


 
Reply With Quote
 
Bob Barrows [MVP]
Guest
Posts: n/a
 
      06-12-2006
Rob Meade wrote:
> "Bob Barrows [MVP]" wrote ...
>
>> nope
>> yup

>
> 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
%>




--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.


 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      06-12-2006
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>



--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      06-12-2006
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.


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
Egbert Nierop \(MVP for IIS\)
Guest
Posts: n/a
 
      06-13-2006

"Rob Meade" <(E-Mail Removed)> wrote in message
news:fOjjg.84962$(E-Mail Removed). uk...
> 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)

--
compatible web farm Session replacement for Asp and Asp.Net
http://www.nieropwebconsult.nl/asp_session_manager.htm

 
Reply With Quote
 
Rob Meade
Guest
Posts: n/a
 
      06-13-2006
"Egbert Nierop (MVP for IIS)" wrote ...

> 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


 
Reply With Quote
 
Rob Meade
Guest
Posts: n/a
 
      06-13-2006
"Evertjan." wrote ...

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


It was - my apologies.

Regards

Rob


 
Reply With Quote
 
Rob Meade
Guest
Posts: n/a
 
      06-13-2006
"Bob Barrows [MVP]" wrote ...

[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
)

Regards

Rob


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
Removing GPO setting from XP machine after removing from Domain Piet Slaghekke Computer Support 4 01-02-2007 08:58 PM
removing a namespace prefix and removing all attributes not in that same prefix Chris Chiasson XML 6 11-14-2006 05:08 PM
char arrays and integer arrays... why the difference? Bill Reyn C++ 3 06-22-2004 12:01 PM
Arrays.asList() returning java.util.Arrays$ArrayList Alexandra Stehman Java 5 06-17-2004 06:04 PM



Advertisments