Jump to content

I cannot write the correct Ubound Index Count to my Array


Go to solution Solved by Nine,

Recommended Posts

Posted

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

 

 

 

Posted

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
Posted

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.

Posted

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?

 

 

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
×
×
  • Create New...