Jump to content

Fastest Way to Delete Empty Records in 1d Array


Go to solution Solved by Andreik,

Recommended Posts

Didn't test the speed so it might be faster with lookaheads. I thought it might be faster because lookaheads sometimes are time expensive but (\|\s*)* it's time expensive as well.

When the words fail... music speaks.

Link to comment
Share on other sites

You could also go about it the other way round. Instead of deleting the empty elements using StringRegExpReplace, return the non-empty elements using StringRegExp.
Then you can do without lookarounds and pack the whole thing into a one-liner:

Func _strip_AspirinJunkie2(ByRef $aArray)
    Return StringRegExp(StringStripWS(_ArrayToString($aArray), 2), "\s+\|(*SKIP)(?!)|[^|]+", 3)
EndFunc

 

Edited by AspirinJunkie
Link to comment
Share on other sites

@AspirinJunkie In some cases won't work:

#include <Array.au3>

Local $array[3]
$array[0] = ''
$array[1] = ''
$array[2] = ' some data  '
$array[1] = '  '

ConsoleWrite(StringLen($array[2]) & @CRLF)
$array = _strip_AspirinJunkie2($array)
ConsoleWrite(StringLen($array[0]) & @CRLF)

_ArrayDisplay($array)

Func _strip_AspirinJunkie2(ByRef $aArray)
    Return StringRegExp(StringStripWS(_ArrayToString($aArray), 2), "\s+\|(*SKIP)(?!)|[^|]+", 3)
EndFunc

 

When the words fail... music speaks.

Link to comment
Share on other sites

Ah yes - i hoped to bypass the "(?>\||\z)"-construct with StringStripWS but didn`t had your case in mind.
So just add this and it should work:

#include <Array.au3>

Local $array[3]
$array[0] = ''
$array[1] = ''
$array[2] = ' some data  '
$array[1] = '  '

ConsoleWrite(StringLen($array[2]) & @CRLF)
$array = _strip_AspirinJunkie2($array)
ConsoleWrite(StringLen($array[0]) & @CRLF)

_ArrayDisplay($array)

Func _strip_AspirinJunkie2(ByRef $aArray)
    Return StringRegExp(_ArrayToString($aArray), "\s+(?>\||\z)(*SKIP)(?!)|[^|]+", 3)
EndFunc

 

Link to comment
Share on other sites

as a note: If you want faster execution, you may need to give up memory efficiency or make the code structure more complex. By carefully evaluating and prioritizing these trade-offs, it's crucial to thoroughly assess different strategies before implementing them. This involves carefully considering potential challenges, time constraints, organizing code, and ensuring compatibility with existing systems to address future issues smoothly.

Func _Deye_ArrayDelEmptyRows(ByRef $aArray, $sDelim = Chr(32), $bUBound = False)
    Local $iArrayColumns = UBound($aArray, 2)
    If $iArrayColumns >= 1 Then
        Local $iCopyTo_Index = 0
        For $i = 0 To UBound($aArray) - 1
            For $j = 0 To $iArrayColumns - 1
;~              If StringStripWS($aArray[$i][$j], 8) Then ExitLoop
                If $aArray[$i][$j] Then ExitLoop
                If $j = $iArrayColumns - 1 Then ContinueLoop 2
            Next
            If $i <> $iCopyTo_Index Then
                For $j = 0 To $iArrayColumns - 1
                    $aArray[$iCopyTo_Index][$j] = $aArray[$i][$j]
                Next
            EndIf
            $iCopyTo_Index += 1
        Next
        If UBound($aArray) > $iCopyTo_Index Then ReDim $aArray[$iCopyTo_Index][$iArrayColumns]
        If $bUBound Then _ArrayInsert($aArray, 0, UBound($aArray))
        Return ($aArray)
    Else
        Return StringSplit(StringTrimLeft(StringRegExpReplace(StringRegExpReplace($sDelim & _ArrayToString($aArray, $sDelim), $sDelim & "[" & $sDelim & "]*[" & $sDelim & "]", $sDelim), $sDelim & "$", ""), 1), $sDelim, $bUBound ? "" : 3)
    EndIf
EndFunc

 

Link to comment
Share on other sites

Your code does something different than the other solution and the task.
Your function not only removes the empty elements but also removes leading and trailing spaces in the fields containing data and also separates the data if a space occurs in them:

#include <Array.au3>

Local $aArray[] = [" ", " This", " ", "is ", " my test ", " "]

$aArray = _Deye_ArrayDelEmptyRows($aArray)

For $i = 0 To UBound($aArray) - 1
    ConsoleWrite("|" & $aArray[$i] & "|" & @CRLF) 
Next


Func _Deye_ArrayDelEmptyRows(ByRef $aArray, $sDelim = Chr(32), $bUBound = False)
    Local $iArrayColumns = UBound($aArray, 2)
    If $iArrayColumns >= 1 Then
        Local $iCopyTo_Index = 0
        For $i = 0 To UBound($aArray) - 1
            For $j = 0 To $iArrayColumns - 1
;~              If StringStripWS($aArray[$i][$j], 8) Then ExitLoop
                If $aArray[$i][$j] Then ExitLoop
                If $j = $iArrayColumns - 1 Then ContinueLoop 2
            Next
            If $i <> $iCopyTo_Index Then
                For $j = 0 To $iArrayColumns - 1
                    $aArray[$iCopyTo_Index][$j] = $aArray[$i][$j]
                Next
            EndIf
            $iCopyTo_Index += 1
        Next
        If UBound($aArray) > $iCopyTo_Index Then ReDim $aArray[$iCopyTo_Index][$iArrayColumns]
        If $bUBound Then _ArrayInsert($aArray, 0, UBound($aArray))
        Return ($aArray)
    Else
        Return StringSplit(StringTrimLeft(StringRegExpReplace(StringRegExpReplace($sDelim & _ArrayToString($aArray, $sDelim), $sDelim & "[" & $sDelim & "]*[" & $sDelim & "]", $sDelim), $sDelim & "$", ""), 1), $sDelim, $bUBound ? "" : 3)
    EndIf
EndFunc

 

Link to comment
Share on other sites

Could you explain this in more detail?
What do you mean by quotation marks (Chr(34)) as delimiters?
The thread was about deleting elements from arrays that consist exclusively of spaces (spaces, tabs, etc.) or empty strings. Spaces within elements with other characters should remain untouched.

Can you show how this task can be implemented with your function using my example array?

Link to comment
Share on other sites

#include <Array.au3>

Local $aArray[] = [" ", " This  ", " ", "is ", "            my test ", " "]

$aArray1 = _Deye_ArrayDelEmptyRows($aArray, Chr(32))
_ArrayDisplay($aArray1)     ; <<<--- trimed all spaces

$aArray2 = _Deye_ArrayDelEmptyRows($aArray, Chr(34))
_ArrayDisplay($aArray2)     ; <<<--- no empty spaces removed

Func _Deye_ArrayDelEmptyRows(ByRef $aArray, $sDelim = Chr(32), $bUBound = False)
    Local $iArrayColumns = UBound($aArray, 2)
    If $iArrayColumns >= 1 Then
        Local $iCopyTo_Index = 0
        For $i = 0 To UBound($aArray) - 1
            For $j = 0 To $iArrayColumns - 1
;~              If StringStripWS($aArray[$i][$j], 8) Then ExitLoop
                If $aArray[$i][$j] Then ExitLoop
                If $j = $iArrayColumns - 1 Then ContinueLoop 2
            Next
            If $i <> $iCopyTo_Index Then
                For $j = 0 To $iArrayColumns - 1
                    $aArray[$iCopyTo_Index][$j] = $aArray[$i][$j]
                Next
            EndIf
            $iCopyTo_Index += 1
        Next
        If UBound($aArray) > $iCopyTo_Index Then ReDim $aArray[$iCopyTo_Index][$iArrayColumns]
        If $bUBound Then _ArrayInsert($aArray, 0, UBound($aArray))
        Return ($aArray)
    Else
        Return StringSplit(StringTrimLeft(StringRegExpReplace(StringRegExpReplace($sDelim & _ArrayToString($aArray, $sDelim), $sDelim & "[" & $sDelim & "]*[" & $sDelim & "]", $sDelim), $sDelim & "$", ""), 1), $sDelim, $bUBound ? "" : 3)
    EndIf
EndFunc

How is this a solve for OP's question?

Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

In order to see if it works, try using the right delimiter, such as ",  " <- holds a space there. You can then refine the code by modifying it to something along the lines of "[" & $sDelim & "][" & $sDelim & "]" to achieve the desired outcome. (maybe maybe)

then anyone interested in conducting their own experiments to discover a solution that fits their specific needs could use this example as a jumping-off point. This is not exactly breaking news, as we know

 

 

Link to comment
Share on other sites

Why should we try out how to solve the problem with your function and not you?
Is your function now a solution for the thread or off-topic?

The annoying thing is the way you deal with the specific and legitimate questions about this:
They are simply ignored with the sentence that the others are just too stupid to enter the correct delimiter.

In short: Then please show us the correct delimiter for the thread task here.

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