• Welcome to Valhalla Legends Archive.
 

Remove Array() by index

Started by CrAz3D, March 22, 2004, 01:47 PM

Previous topic - Next topic

CrAz3D

I am aware that this has recently been posted, yet does not function to my needs.


I have my Array
Quotehttp://
feany
gay
bob

I can remove the first Array index(http://) correctly.  However, if I try to remove the second index(feany) it removes the http:// aswell.  But wait, there's more.  When I remove the last index(bob) it just duplicates the first index(http://) aswell.

Remove Code
For i = 0 To UBound(PhraseList) - 1
               If LCase(pA(1)) = LCase(PhraseList(i)) Then
                   SendW "Removed phrase: " & PhraseList(i), strAccount
                   PhraseList(i) = vbNullString
                   ReDim Preserve PhraseList(UBound(PhraseList) - 1)
               End If
           Next i


Help is appreciatd!  ::)
rebundance - having or being in excess of sheer stupidity
(ré-bun-dance)
Quote from: Spht on June 22, 2004, 07:32 PMSlap.
Quote from: Adron on January 28, 2005, 09:17 AMIn a way, I believe that religion is inherently evil, which includes Christianity. I'd also say Christianity is eviller than Buddhism (has more potential for evil).
Quote from: iago on April 19, 2005, 01:06 PM
CrAz3D's ... is too big vertically, at least, too big with ... iago ...

Stealth

Here's the most basic way to remove an item from an ARRAY. If you'll be doing it often you may want to look into a collection.

1. Create a new temporary array, dimensioned to (size - 1) of your start array.
2. Loop throguh your start array, copying everything but the index you want to remove. The temp array now contains your entire start array except the item you want to remove.
3. Redimension your start array to the size of the temp array.
4. Copy the temp array into the start array.
- Stealth
Author of StealthBot

CrAz3D

Quote from: Stealth on March 22, 2004, 02:04 PM
Here's the most basic way to remove an item from an ARRAY. If you'll be doing it often you may want to look into a collection.

1. Create a new temporary array, dimensioned to (size - 1) of your start array.
2. Loop throguh your start array, copying everything but the index you want to remove. The temp array now contains your entire start array except the item you want to remove.
3. Redimension your start array to the size of the temp array.
4. Copy the temp array into the start array.
I'll try a collection...thnx
rebundance - having or being in excess of sheer stupidity
(ré-bun-dance)
Quote from: Spht on June 22, 2004, 07:32 PMSlap.
Quote from: Adron on January 28, 2005, 09:17 AMIn a way, I believe that religion is inherently evil, which includes Christianity. I'd also say Christianity is eviller than Buddhism (has more potential for evil).
Quote from: iago on April 19, 2005, 01:06 PM
CrAz3D's ... is too big vertically, at least, too big with ... iago ...

Grok


Lenny

Just a note, collections are much slower and less efficient than arrays, they should be avoided in general...

In addition, you could merely overwrite the array(index) you want to erase with array(index+1) and loop this, after this is finished you resize the array to (Ubound - 1) because the last array value will be a copy of the second to last....
The Bovine Revolution
Something unimportant

Live Battle.net:

WARNING: The preceding message may have contained content unsuitable for young children.

Stealth

Quote from: Lenny on March 22, 2004, 10:01 PM
Just a note, collections are much slower and less efficient than arrays, they should be avoided in general...

In addition, you could merely overwrite the array(index) you want to erase with array(index+1) and loop this, after this is finished you resize the array to (Ubound - 1) because the last array value will be a copy of the second to last....

That is indeed more efficient than I described. Does it work in all situations?
- Stealth
Author of StealthBot

Flame

I did something similar to what Lenny said, but only for one purpose (coincidence, Phraseban!) and it seems to work fine.  I can't explain it too well... but I'll just paste my code up here to see if you can get the idea.  I haven't used it a lot, but with the little I have used it, it functions well.


           Dim i As Integer
           Dim RFP As Integer
           If Phrasebans(0) = "" Then Exit Sub
           For i = 0 To UBound(Phrasebans)
               If LCase(Mid$(Message, 11)) = LCase(Phrasebans(i)) Then
                   Phrasebans(i) = ""
                   For RFP = i To UBound(Phrasebans)
                       If Phrasebans(RFP) = "" And RFP < UBound(Phrasebans) Then
                           Phrasebans(RFP) = Phrasebans(RFP + 1)
                           Phrasebans(RFP + 1) = ""
                       End If
                   Next RFP
                   If UBound(Phrasebans) = 0 Then
                       Phrasebans(0) = ""
                   Else
                       ReDim Preserve Phrasebans(0 To UBound(Phrasebans) - 1)
                   End If
                   Exit For
               End If
           Next i

Stealth

That appears to be exactly what Lenny described.
- Stealth
Author of StealthBot

Fr0z3N

uhh, I think Stealth has a good way, thats what I use.

o.OV

#9
I read collections are slower if working
with about 100 items or less.
I don't use collections
so I can't give an opinion on that.

If the array does not need to be
in an order of any kind.
If a string.. switch the descriptor with last item
If a value.. item to remove equal to last item
last item not neccessarily the ubound,
and then zero out the last item
to kill off the extra item,
depending on how you want to handle it.
If the facts don't fit the theory, change the facts. - Albert Einstein

Adron

Quote from: Lenny on March 22, 2004, 10:01 PM
Just a note, collections are much slower and less efficient than arrays, they should be avoided in general...

Where did you find this information? I was assuming that collections were some kind of binary tree which would make them very efficient for things like removing an item from the middle, looking up an item from a key, growing and shrinking.


CrAz3D

#12
From what I see an Array is better in most opinions & according to facts.  I will redo it to use an Array instead, thank you all.


There still seems to be a few problems that I cannot fiugre out.
Quote[8:05:42 AM] «CrAz3D[xL]» \flist
[8:05:42 AM] «0x69» Current Filters are: http, stealth, bob, gay, feany, telrin
[8:05:46 AM] «CrAz3D[xL]» \frem http
[8:05:46 AM] «0x69» Removed filter: http
[8:05:48 AM] «CrAz3D[xL]» \flist
[8:05:48 AM] «0x69» Current Filters are: stealth, bob, gay, feany, telrin
[8:05:53 AM] «CrAz3D[xL]» \frem telrin
[8:05:53 AM] «0x69» Removed filter: telrin
[8:05:55 AM] «CrAz3D[xL]» \flist
[8:05:55 AM] «0x69» Current Filters are: bob, gay, feany, feany
For i = 0 To UBound(FilterList) - 1
               If LCase(pA(1)) = LCase(FilterList(i)) Then
                   SendW "Removed filter: " & FilterList(i), strAccount
                   FilterList(i) = FilterList(i + 1)
                   ReDim Preserve FilterList(UBound(FilterList) - 1)
               End If
           Next i

I have a feeling the it is the "ReDim Preserve" portion that I am screwing up.
rebundance - having or being in excess of sheer stupidity
(ré-bun-dance)
Quote from: Spht on June 22, 2004, 07:32 PMSlap.
Quote from: Adron on January 28, 2005, 09:17 AMIn a way, I believe that religion is inherently evil, which includes Christianity. I'd also say Christianity is eviller than Buddhism (has more potential for evil).
Quote from: iago on April 19, 2005, 01:06 PM
CrAz3D's ... is too big vertically, at least, too big with ... iago ...

drivehappy

#13
Your ReDim statement appears correct.


FilterList(i) = FilterList(i + 1)

This however only moves the next element into the current's position - not all subsequent elements in the array. If 'i' is at the end of the array an out of bounds error may occur if you try accessing the next element that is not there. I cannot see how your first element is removed though.

CrAz3D

hmm, ok...I'll mess w/the Filterlist - w/e
rebundance - having or being in excess of sheer stupidity
(ré-bun-dance)
Quote from: Spht on June 22, 2004, 07:32 PMSlap.
Quote from: Adron on January 28, 2005, 09:17 AMIn a way, I believe that religion is inherently evil, which includes Christianity. I'd also say Christianity is eviller than Buddhism (has more potential for evil).
Quote from: iago on April 19, 2005, 01:06 PM
CrAz3D's ... is too big vertically, at least, too big with ... iago ...