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

Bruce Steers bsteers4 at gmail.com
Wed Jun 21 13:12:28 CEST 2023


On Wed, 21 Jun 2023 at 12:08, Bruce Steers <bsteers4 at gmail.com> wrote:

>
>
> 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
>

Or simpler just apply direct to the array...

Public Sub Form_Open()

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

Public Sub ArrayClean(hArray As Variant)

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

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


More information about the User mailing list