Andreik Posted April 23 Share Posted April 23 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 More sharing options...
AspirinJunkie Posted April 24 Share Posted April 24 (edited) 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 April 24 by AspirinJunkie Link to comment Share on other sites More sharing options...
Andreik Posted April 24 Share Posted April 24 @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 AspirinJunkie 1 When the words fail... music speaks. Link to comment Share on other sites More sharing options...
AspirinJunkie Posted April 24 Share Posted April 24 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 Andreik 1 Link to comment Share on other sites More sharing options...
Andreik Posted April 24 Share Posted April 24 @AspirinJunkie Nicely done and performs really good. When the words fail... music speaks. Link to comment Share on other sites More sharing options...
Nine Posted April 24 Share Posted April 24 (edited) nvm - not totally woke-up, @AspirinJunkie good job. Edited April 24 by Nine “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
AspirinJunkie Posted April 24 Share Posted April 24 With this solution, this is basically only possible by separating and treating them individually. Link to comment Share on other sites More sharing options...
Deye Posted April 30 Share Posted April 30 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 More sharing options...
AspirinJunkie Posted April 30 Share Posted April 30 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 More sharing options...
Deye Posted April 30 Share Posted April 30 AspirinJunkie, This function relies on the user using the correct delimiter Chr(34) Link to comment Share on other sites More sharing options...
Andreik Posted April 30 Share Posted April 30 @Deye take your time to understand what OP asked for and what @AspirinJunkie pointed out. When the words fail... music speaks. Link to comment Share on other sites More sharing options...
AspirinJunkie Posted April 30 Share Posted April 30 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 More sharing options...
Deye Posted April 30 Share Posted April 30 apologies, but in order to try your hand at this function fairly , you must pass the correct arguments, especially the delimiter being used. like so, _Deye_ArrayDelEmptyRows($aArray, Chr(34)) or _Deye_ArrayDelEmptyRows($aArray, '"') AspirinJunkie 1 Link to comment Share on other sites More sharing options...
Andreik Posted April 30 Share Posted April 30 (edited) #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 April 30 by Andreik AspirinJunkie 1 When the words fail... music speaks. Link to comment Share on other sites More sharing options...
Deye Posted April 30 Share Posted April 30 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 More sharing options...
AspirinJunkie Posted May 1 Share Posted May 1 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. Andreik 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now