Jump to content

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

EndIf

Next

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?

Link to comment
Share on other sites

Hi,

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)

Next

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)
    Next
    Return StringSplit(StringTrimRight($sTmp, 1), Chr(0))
EndFunc

Cheers

Edited by smashly
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...