[Gambas-user] Remove Null or Empty items in arrays.

Bruce Steers bsteers4 at gmail.com
Wed Jun 21 13:08:27 CEST 2023


On Wed, 21 Jun 2023 at 10:47, Martín <mbelmonte at belmotek.net> wrote:

> Hi,
>
> Would it be possible to add a method for Array class to remove empty or
> null items?
> It would act similar to the Remove method but without input parameters.
>
> Thanks.
>
> Martín.


it's a pretty simple function for most types
Something like this...

Public Sub Form_Open()

  Dim aNums As Integer[] = [1, 3, 5, 0, 4, 0, 3]
  Print aNums.Count
  aNums = ArrayClean(aNums)
  Print aNums.Count
End


Public Sub ArrayClean(MyArray As Variant) As Variant

  Dim hArray As Variant = MyArray.Copy()
  For c As Integer = hArray.Max DownTo 0
    If Not hArray[c] Then hArray.Remove(c)
  Next

  Return hArray

End

Prints...
7
5

BruceS
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20230621/f9896677/attachment.htm>


More information about the User mailing list