UnderwareDESIGN

PlayBASIC => Beginners => Topic started by: Alex on March 27, 2007, 04:19:53 PM

Title: removing an array cell completely?
Post by: Alex on March 27, 2007, 04:19:53 PM
hi,
is it possible to remove an array cell completely(not just setting it to zero)?
How would I go abouts doing this for a type array
I tried copying the entire array to another, and then only copying the values i wanted back into the original, and then redimming it, but this didn't work.
Title: Re: removing an array cell completely?
Post by: kevin on March 28, 2007, 08:23:42 AM
 You can copy cells within an array, but you have to careful how the ranges overlap.. Bellow we have two methods, the first copies the following cells down over the removed cell, then resizes the array, the second just copies the last item down over the removed cell and resizes.  The second method doesn't keep the order in the tact though.

[pbcode]

max=10
Dim Table(10)

For lp=0 to max
    Table(lp)=1000+lp
next

    ShowArray(Table())

   RemoveCell(Table(),5)

    ShowArray(Table())

   RemoveCell2(Table(),9)

    ShowArray(Table())

   Sync
   waitkey
   

Function ShowArray(Me())
   Print "Array"
   For lp=0 to Getarrayelements(me(),1)
      t$=t$+Str$(Me(lp))+","
   next
   print TrimRight$(T$,",")
   
EndFunction


Function RemoveCell(Me(),Index)
   Size=getarrayelements(me(),1)
   ; Some safe code to to Clip Illegal values
   if Index<0       Then Index=0
   If Index<(Size+1)
      // Copy Cell Down Method
      CopyArrayCells  Me(),Index+1,1,Me(),Index,1,Size      
      Redim Me(Size-1)      
   Endif         
EndFunction



Function RemoveCell2(Me(),Index)
   Size=getarrayelements(me(),1)
   ; Some safe code to to Clip Illegal values
   if Index<0       Then Index=0
   If Index<(Size+1)
      // Copy Last Cell to Remove point then resize
      CopyArrayCells  Me(),Size,1,Me(),Index,1,1      
      Redim Me(Size-1)      
   Endif         
EndFunction

[/pbcode]


Related To:

      * Pack Array (http://www.underwaredesign.com/forums/index.php?topic=4193.0)





Title: Re: removing an array cell completely?
Post by: Alex on March 28, 2007, 04:49:54 PM
thanks, that helped a lot.