How to dynamically REMOVE an item from an array

Results 1 to 2 of 2

Thread: How to dynamically REMOVE an item from an array

  1. #1
    Rory Lysaght Guest

    Default How to dynamically REMOVE an item from an array

    I&#039m adding items to an array which is passed as session variable. I can add items to the array, and set the quantity in a corresponding array to zero. But how can I remove an item from within the array, then have all subsequent items re-indexed?<BR><BR>Example:<BR>SKUArray QtyArray<BR>10001 . . . 1<BR>10119 . . . 5<BR>10582 . . . 0 &#060;-- since this item qty is zero, I want to remove it<BR>10469 . . . 2<BR>10737 . . . 1<BR><BR>Should become:<BR>10001 . . . 1<BR>10119 . . . 5<BR>10469 . . . 2<BR>10737 . . . 1<BR><BR>Thanks,<BR> Rory

  2. #2
    Join Date
    Dec 1969

    Default RE: How to dynamically REMOVE an item from an arra

    This is a classic computer programming exercise! There does not exist any function that will easily do this. The algorithm goes like this:<BR><BR>Create two indexes to the array, Current and InsertPosition, and set them to both point to the beginning of the array<BR>Loop Current from the beginning of the end of the array<BR>If the array element at Current = 0, then increment Current<BR>If the array element at Current is non-zero, put the current element in InsertPostion&#039s position, and increment both Current and InsertPosition<BR><BR>When completed, you&#039ll have an array with the 0 elements removed. However, it is vitally important to realize that the array&#039s dimensions have not changed! You still have an array with as many elements as their use to be, however, the elements beyond InsertPosition are garbage. So, you can either keep track of this fact, or use ReDim Preserve to hack off these annoying end elements (see for the technical docs on ReDim)<BR><BR>So, let&#039s write some code! (Note that I do not ensure the working order of this code, it is off the top of my head!)<BR><BR>&#039Remove the 0 elements from array SKUArray and compact the array<BR>Dim iCurrent, iInsertPosition<BR>iInsertPosition = LBound(SKUArray)<BR><BR>&#039Loop from the beginning of the loop to the end<BR>For iCurrent = LBound(SKUArray) to UBound(SKUArray)<BR><BR>&#039Check to see if SKUArray(iCurrent) = 0<BR>If SKUArray(iCurrent) = 0 then<BR><BR>&#039We need to increment current<BR>iCurrent = iCurrent + 1<BR><BR>Else<BR><BR>&#039We need to put SKUArray(iCurrent) into SKUArray(iInsertPosition) and increment both iInsertPosition and iCurrent<BR>SKUArray(iInsertPosition) = SKUArray(iCurrent)<BR><BR>iInsertPosition = iInsertPosition + 1<BR>iCurrent = iCurrent + 1<BR><BR>End If<BR><BR>Loop<BR><BR>&#039Now, we need to compact the array<BR>ReDim Preserve SKUArray(iInsertPosition)<BR><BR><BR>Hope this helps! Happy Programming!<BR><BR>(BTW, you can see a modified version of the algorithm that compacts a sorted array with duplicates at:<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts