EmilyLove Posted September 4, 2021 Posted September 4, 2021 I have a string containing the full path of an executable and an array of executables without their paths. I am trying to compare the string to the list in the array and if a match is found, remove it from the array. The entry get removed from the array successfully, and after checking its return result, uses it to update the ubound if it succeeded, but it doesn't want to update to the new value. Any ideas what I am doing wrong? It acts like it is read-only. #include <Array.au3> #include <File.au3> Local $sApp_Exe = "F:\App\Nextcloud\nextcloud.exe" Local $aWaitForEXEX = [3, "Nextcloud.exe", "nextcloudcmd.exe", "QtWebEngineProcess.exe"] For $h = 1 To $aWaitForEXEX[0] If StringInStr($sApp_Exe, $aWaitForEXEX[$h]) <> 0 Then $iRet = _ArrayDelete($aWaitForEXEX, $h) If $iRet <> -1 Then $aWaitForEXEX[0] = $iRet ;this line doesn't work. $aWaitForEXEX[0] doesn't update and shortly gives Error: Array variable has incorrect number of subscripts or subscript dimension range exceeded.: _ArrayDisplay($aWaitForEXEX) EndIf Next
Danny35d Posted September 4, 2021 Posted September 4, 2021 Give it a try #include <Array.au3> #include <File.au3> Local $iCount = 0 Local $sApp_Exe = "F:\App\Nextcloud\nextcloud.exe" Local $aWaitForEXEX = [5, "Nextcloud.exe", "nextcloudcmd.exe", "Nextcloud.exe", "QtWebEngineProcess.exe", "Nextcloud.exe"] For $h = 1 To $aWaitForEXEX[0] If StringInStr($sApp_Exe, $aWaitForEXEX[$h]) Then $iRet = _ArrayDelete($aWaitForEXEX, $h) If $iRet <> -1 Then $h = 1 $iCount += 1 EndIf EndIf If $h >= $aWaitForEXEX[0] - $iCount Then ExitLoop Next $aWaitForEXEX[0] = UBound($aWaitForEXEX) - 1 _ArrayDisplay($aWaitForEXEX) AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line
Solution Nine Posted September 4, 2021 Solution Posted September 4, 2021 It is kind of "dangerous" to delete items in an array going upward. When you delete an item the array is rearranged and the indice needs to be adjusted. @Danny35d showed you one "complicated" way. But the easiest way is to delete items going downward : #include <Array.au3> Local $sApp_Exe = "F:\App\Nextcloud\nextcloud.exe" Local $aWaitForEXEX = [4, "Nextcloud.exe", "Nextcloud.exe", "nextcloudcmd.exe", "QtWebEngineProcess.exe"] For $h = $aWaitForEXEX[0] To 1 Step -1 If StringInStr($sApp_Exe, $aWaitForEXEX[$h]) Then $iRet = _ArrayDelete($aWaitForEXEX, $h) If $iRet <> -1 Then $aWaitForEXEX[0] = $iRet - 1 EndIf Next _ArrayDisplay($aWaitForEXEX) Notice the 0 element is adjusted correctly (subtracted by 1). As help file explains, the return value of _ArrayDelete includes the 0 element. “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) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
EmilyLove Posted September 8, 2021 Author Posted September 8, 2021 Both solutions work. @Nine makes an excellent point. I always forget I can go backwards on my for next loops! Anywho, any idea why $aWaitForEXEX[0] wouldn't update within the loop?
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