[ASP] Arrays - removing an item?

Discussion in 'ASP General' started by Rob Meade, Jun 12, 2006.

  1. Rob Meade

    Rob Meade Guest

    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
     
    Rob Meade, Jun 12, 2006
    #1
    1. Advertising

  2. 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.
     
    Bob Barrows [MVP], Jun 12, 2006
    #2
    1. Advertising

  3. Rob Meade

    Rob Meade Guest

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

    :eek:(
     
    Rob Meade, Jun 12, 2006
    #3
  4. 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.
     
    Bob Barrows [MVP], Jun 12, 2006
    #4
  5. Rob Meade

    Evertjan. Guest

    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)
     
    Evertjan., Jun 13, 2006
    #5
  6. Rob Meade

    Evertjan. Guest

    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)
     
    Evertjan., Jun 13, 2006
    #6
  7. "Rob Meade" <> wrote in message
    news:fOjjg.84962$...
    > 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
     
    Egbert Nierop \(MVP for IIS\), Jun 13, 2006
    #7
  8. Rob Meade

    Rob Meade Guest

    "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
     
    Rob Meade, Jun 13, 2006
    #8
  9. Rob Meade

    Rob Meade Guest

    "Evertjan." wrote ...

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


    It was - my apologies.

    Regards

    Rob
     
    Rob Meade, Jun 13, 2006
    #9
  10. Rob Meade

    Rob Meade Guest

    "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
    :eek:)

    Regards

    Rob
     
    Rob Meade, Jun 13, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RA
    Replies:
    0
    Views:
    4,594
  2. =?Utf-8?B?Q2hyaXM=?=

    removing item in dropdownlist

    =?Utf-8?B?Q2hyaXM=?=, Jan 12, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    32,301
    swati254
    Jun 9, 2008
  3. Chris  Chiasson
    Replies:
    6
    Views:
    637
    Richard Tobin
    Nov 14, 2006
  4. Philipp
    Replies:
    21
    Views:
    1,167
    Philipp
    Jan 20, 2009
  5. Ann
    Replies:
    0
    Views:
    168
Loading...

Share This Page