?
Solved

Remove items from an array

Posted on 2005-03-14
23
Medium Priority
?
346 Views
Last Modified: 2008-03-10
Hi all,

I have a made a simple "cart" application. I want to be able to press a 'Delete' button which will be listed alongside each item within the cart contents (which is an array).

I then want it to delete the item from the array, not to sure how to do this so i'm stuck at the moment.

Thanks
0
Comment
Question by:koopddesign
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
  • 6
23 Comments
 
LVL 14

Expert Comment

by:kiddanger
ID: 13535383
Hi koop...

I have a quick and dirty solution I worked out a couple of days ago.

function removeArrayElement(arr, x)
  removeArrayElement = filter(arr, x, false)
end function

Call it with:

dim d, f
' d = array number - remember arrays are zero based
d = 1
f = removeArrayElement(myarr, myarr(d))

It returns the array in 'f' without the element you chose to remove.

HTH...
0
 

Author Comment

by:koopddesign
ID: 13535583
Thanks for that, i love the coding, very simple, it seems to work ok but i'm getting stange behaviour now, please see, http://www.koopd.com/sessions/

it automatically creates 2 cart items, now it works ok if you delete any one of those items but if you press add to the array a few times and then try and delete the last item in the array it deletes all the items that were added when 'add to the array' was clicked.

Confused!
0
 
LVL 19

Expert Comment

by:peh803
ID: 13536028
Yeah -- the reason it's doing that is that the filter method will remove all occurrences in the array that match the string passed.  So, if you copy "hello there" 20 times into elements 2 through 21, then you filter out "hello there", it will remove all occurrences of "hello there".

If you want to remove an item by a specified index number from a 1D array, you'll need a slightly different custom function.  

Let me write one up real quick..
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:koopddesign
ID: 13536085
Thanks, using the index number sounds like the better option!
0
 
LVL 19

Assisted Solution

by:peh803
peh803 earned 100 total points
ID: 13536106
here's a quick example -- I've tested it and it seems to work!

<%
Dim theArray : theArray = Array("baseball", "football", "soccer", "volleyball", "pool", "water polo")
for i=0 to ubound(theArray)
  response.Write i & ": " & theArray(i) & "<BR>"
next
Dim vNewArray
vNewArray = removeArrayElement(theArray, 2)
response.Write "<br>new array <BR>"
for i=0 to ubound(vNewArray)
  response.Write i & ": " & vNewArray(i) & "<BR>"
next


function removeArrayElement(vMyArray, lElementIndexToRemove)
  Dim vRetArray
  Dim lUBound : lUBound = ubound(vMyArray)
  Dim lNewArrCount
  Dim lOldArrCount
  redim vRetArray((lUBound-1))
  for i=0 to ubound(vMyArray)
    if lElementIndexToRemove < i then
      lNewArrCount=i-1
    else
      lNewArrCount=i
    end if
    if i<>lElementIndexToRemove then
      vRetArray(lNewArrCount) = vMyArray(i)
    end if
  next
  removeArrayElement = vRetArray
end function

%>
0
 
LVL 14

Expert Comment

by:kiddanger
ID: 13536248
Yes, peh is correct.  This is only good if the array elements are unique.

Here is an indexed version:

function removeArrayElement(arr, x)
  dim d, i
  set d = Server.CreateObject("Scripting.Dictionary")
  for i = 0 to ubound(arr)
    d.Add i, arr(i)
  next
  d.Remove x
  removeArrayElement = d.Items
  set d = nothing
end function
0
 
LVL 14

Accepted Solution

by:
kiddanger earned 100 total points
ID: 13536300
To pass to the indexed version use:

quick sample...

dim a, b
b = 1
a = split("x o x o x o x o")

b = removeArrayElement(a, d)

function removeArrayElement(arr, x)
  dim d, i
  set d = Server.CreateObject("Scripting.Dictionary")
  for i = 0 to ubound(arr)
    d.Add i, arr(i)
  next
  d.Remove x
  removeArrayElement = d.Items
  set d = nothing
end function
0
 
LVL 19

Expert Comment

by:peh803
ID: 13536357
>>b = removeArrayElement(a, d)

You haven't defined "d" -- simple mistake...

So, the above example should be something more like this:


<%
dim a, b, d, i
b = 1
d = 4  'remove 4th item in the array
a = split("x o x o x o x o")

response.write "original array: <BR>"
for i=0 to ubound(a)
  response.write i & ": " & a(i)
next

b = removeArrayElement(a, d)

response.write "array with item #" & d & " removed by index: <BR>"
for i=0 to ubound(b)
  response.write i & ": " & b(i)
next

function removeArrayElement(arr, x)
  dim d, i
  set d = Server.CreateObject("Scripting.Dictionary")
  for i = 0 to ubound(arr)
    d.Add i, arr(i)
  next
  d.Remove x
  removeArrayElement = d.Items
  set d = nothing
end function
%>

For the record, @kiddanger, I like your removeArrayElement function better than mine :)
0
 

Author Comment

by:koopddesign
ID: 13536433
Thanks guys, i'm going to have to come back to this in a second I've got some work to get out of the way before I can test stuff out.
0
 
LVL 19

Expert Comment

by:peh803
ID: 13536456
Sounds good -- both solutions provided work -- it's just up to you as to which one you want to use.
0
 
LVL 14

Expert Comment

by:kiddanger
ID: 13536561
Thanks Peh.  (O:=

I noticed you beat me to the draw.  Guess I'm going to have to type faster.
I think he'll be happy with either one.

Ironic the remove array function was a side proc.  I wrote an insert array element, not at the end, but in the middle anywhere, and thought I needed a compliment to it.  I don't use it with similar records so filter works fine.  I should have realized he might not have the same scenario.  Thanks for stepping in.
0
 
LVL 19

Expert Comment

by:peh803
ID: 13536601
eh, no problem at all!

I'm always a big fan of trying to write as little code as possible, and that's why I like yours better.  Not sure how the server resources consumed compare between your solution (instantiating an object) and my less vigorous, object-less solution.

Either way, they're both fast enough to not worry about it.

See you around,
Phil / peh803
0
 
LVL 14

Expert Comment

by:kiddanger
ID: 13536646
Actually, I typed that in here really quick so it does have an error...

dim a, b, d

d = 1 ' or whatever number to index

...the rest the same...

To show the array, I was doing this...

assuming b is the array returned....

Response.Write join(b,vbCrLf)

The reason I use vbCrLf is because I originally wrote my insert array element to work with text files, like log files, that are not XML.

Read the whole file in and convert it to an array, insert the new line, join it back to a string with vbCrLf's and write the string back out to a file.

I just used a simple character array as an example...
0
 
LVL 19

Expert Comment

by:peh803
ID: 13536670
>>Actually, I typed that in here really quick so it does have an error...

I noticed that you had the error, and I actually already corrected that for you (5 posts up) :)
0
 
LVL 14

Expert Comment

by:kiddanger
ID: 13536963
Yes I saw that, that's why I was commenting on it because you noticed it, which I appreciate, and also because b was being assigned to the array returned, so it didn't need to be = 1, just dimensioned.  That should have been d=1.  There were two errors. *shrug*

Sorry my response was not more clear...
Thanks again Phil...
0
 
LVL 19

Expert Comment

by:peh803
ID: 13537562
gotcha -- no problem at all.  Either way, both functions provided work!

see you around,
Phil / peh803
0
 

Author Comment

by:koopddesign
ID: 13605693
Hi guys, sorry for taking so long to get back to this, I got severly sidetracked on a major project. Ok I now have this little bit of code but it's returning the error:

Microsoft VBScript runtime error '800a802b'

Element not found

/sessions/index.asp, line 14


function RemoveCartItem(arr, x)
      dim d, i
      set d = Server.CreateObject("Scripting.Dictionary")
      for i = 0 to ubound(arr)
            d.Add i, arr(i)
      next
      d.Remove x                              <---- This is line 14
      RemoveCartItem = d.Items
      set d = nothing
end function

Any ideas?

Thanks!
0
 
LVL 14

Expert Comment

by:kiddanger
ID: 13619481
Hi koop...

Can you show how you're calling the function?

It should be similar to:

x = 3 ' remove 4th array element - array's are zero indexed
arrNew = removeArrayElement(arr, x)

If arr = "a b c d e f g h i j k l m n o p q r s t u v w x y z"
You can use this to make it an array:

dim arr, arrNew, x
x = 3
arr = split("a b c d e f g h i j k l m n o p q r s t u v w x y z") ' space is the default delimiter
arrNew = removeArrayElement(arr, x)

' To verify element removed:
Response.Write arr(x)
' To verify current array elements:
Response.Write join(arrNew,vbCrLf)

d would be removed as it is the 4th array element or an index of 3 (0, 1, 2, 3 ... a, b, c, d ...)

x cannot be greater than the upper boundary of the array.
You could test it before calling it or put it in the function...

If x > ubound(arr) then
  Response.Write "index: " & x & " > array upper boundary: " & ubound(arr)
  Response.End
else
  ' rest of function
end if

0
 

Author Comment

by:koopddesign
ID: 13620265
Thanks for the reply kiddanger, I found out that I had to use the CInt function to turn my Request.Querystring value into and integer rather than a string, it now works perfectly using your method. I'm not to sure who to award the points to as both methods do work although I prefer your cleaner method, would you be happy with a point split?

Thanks again
0
 
LVL 14

Expert Comment

by:kiddanger
ID: 13640413
koop...

Glad to hear it's working out for you.

Yes, QueryString values are always strings.  I've run into that before myself.  It's not what you know but what you remember!  (O:=

Re: points... I believe that decision is up to you.  I see no problem in splitting points as both of us have made an effort and both do work, even though Phil was first. *smirk*  As long as Phil doesn't have an issue with me getting 1/2 the points, I have none.  (O:=
0
 

Author Comment

by:koopddesign
ID: 13659986
OK, I was waiting to here from peh but I have gone ahead and split the points, two great solutions! Thanks!
0
 
LVL 19

Expert Comment

by:peh803
ID: 13663308
hey guys -- sorry -- I've been out of town.  

I never have a problem with a point split as long as it's explained, and as long as both answers accepted in the split indeed contributed different info to the question, etc.  This is certainly the case here, but I really do appreciate the courtesy of asking.  

See you around!

Best regards,
Phil / peh803
0
 
LVL 14

Expert Comment

by:kiddanger
ID: 13663871
Thanks koop.  Welcome back Phil.
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question