[Gambas-user] Better way to delete element from array when enumerating

Benoit Minisini gambas at ...1...
Sun Dec 21 17:35:45 CET 2008


On jeudi 18 décembre 2008, Jussi Lahtinen wrote:
> Hi!
>
> I have code like this:
>
> Dim element as ClassX
>
> FOR EACH element IN objectarray
> IF element = something THEN
> objectarray.Remove(objectarray.Find(element))
> ENDIF
> NEXT
>
> This works, but I'm almost sure that there is better way to do this.
> Feel kind of dumb to find same element two times...
>
>
> Jussi
>

DO
  iInd = objectarray.Find(element)
  IF iInd < 0 THEN BREAK
  objectarray.Remove(iInd)
LOOP

Or:

iInd = 0
WHILE iInd < objectarray.Count
  IF objectarray[iInd] = element THEN
    objectarray.Remove(iInd)
  ELSE
    INC iInd
  ENDIF
WEND

-- 
Benoit Minisini




More information about the User mailing list