Jump to content
Sign in to follow this  

removing nulls from array

Recommended Posts


I have a very simple 1d array that contains a list of entries. This list of entries is being read from a file through filereadfromarray. Because it is possible that the entries in the list may contain blank lines, my array has empty elements scattered throughout it. These entries are null entries but still occupy a spot on my array.

I sort the array, placing the blanks either at the top or the bottom, but how can I remove them efficiently? I can't use arraysearch because you can't search on null.

I also tried something like this

$z = $avArray[0]

for $x = 1 to $z

if stringlen($avArray[$x]) = 0 Then

_ArrayDelete($avArray, $x)

$z = $z -1



The problem is that it bombs out because it doesn't recognize the fact that $z is decreasing.

Anyone else got a good way to remove the nulls?

Share this post

Link to post
Share on other sites

You say you can sort it so the blanks are at the top or bottom? Well, sort it so they're at the bottom, then find the first blank, and reDim to that size.

For $i = 0 to UBound($array-1)
  If $array[$i] = '' Then
    ReDim $array[$i]

Share this post

Link to post
Share on other sites


Since your array is decreasing in size as elements are deleted then you should start at the end of your array and work backwards to element 0.

For $x = Ubound($avArray) - 1 to 0 Step -1

if stringlen($avArray[$x]) = 0 Then _ArrayDelete($avArray, $x)


Or myself I use something like this

#include <Array.au3> ; Only needed for _ArrayDisplay.

Dim $avArray[10] = ["Start", @CR, @LF, @CRLF, @Tab, Chr(0), "Not Blank", "0", "   ", "End"]

$aRet = _ArrayRemoveBlanks($avArray)

_ArrayDisplay($aRet, "$aRet")

; Removes Elemets that contain only whitespace characters and returns the new array.
; The count of the return is at $aRet[0].
Func _ArrayRemoveBlanks($aID)
    Local $sTmp = ''
    For $i = 0 to Ubound($aID) -1
        If StringRegExpReplace($aID[$i], "\s", "") Then $sTmp &= $aID[$i] & Chr(0)
    Return StringSplit(StringTrimRight($sTmp, 1), Chr(0))


Edited by smashly

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this