Jump to content

Array variable has incorrect number of subscripts or subscript dimension range exceeded


Recommended Posts

Can anyone please help me. The following code worked the first time, but errors out now. Any ideas why?

Error:

test.au3 (37) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

Code:

Func _ListComputers()
   Local $rArray, $Buff, $Pid = Run(@ComSpec & ' /c net view', '', @SW_HIDE, 6)
   While Not @error
      $buff &= StdoutRead($Pid)
   WEnd
   $rArray = StringRegExp($Buff, "[]+([w-]+)", 3)
   If @Error Then $rArray = StringRegExp(@ComputerName, "(.+)", 1)
   Return $rArray
EndFunc

$aComputers = _ListComputers()
$sData = ""
For $i = 0 To Ubound($aComputers) -1
    $sData &= $aComputers[$i] & "|"
Next
GuiCtrlSetData($Combo1, StringTrimRight($sData, 1), $aComputers[1])
Edited by BryanVest
Link to comment
Share on other sites

  • Moderators

BryanVest,

You need to do some errorchecking to make sure you get an array returned from the function before you try to access it - something along these lines: :)

$aComputers = _ListComputers();; That's the function posted above.
If IsArray($aComputers) Then
    $sData = ""
    For $i = 0 To Ubound($aComputers) -1
        $sData &= $aComputers[$i] & "|"
    Next
Else
    ; Code to cope with an error when no function is returned
Endif

all clear? ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

BryanVest,

Which part gives you the error? It should be highlighted in the SciTE console. :)

I would suggest the array access is the culprit - you should only try to access the array if you are sure that it exists. ;)

M23

P.S. And to answer the question you originally posted - I have no idea why it only worked once, but with so little code to work on it is very difficult to offer a sensible opinion anyway. :)

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Here is the full code. Please let me know if you can make any sense of what is going on. It worked 1 time for me and now it just errors out. I have spent like 45 minutes trying to figure this out, but I am stuck. Thank you for your help so far.

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=C:Documents and SettingsbvestDesktopkoda_1.7.3.0Formssd.kxf
$Form1 = GUICreate("Symantec Uninstall Tool", 475, 168, 225, 231)
$Label1 = GUICtrlCreateLabel("Computer Name:", 24, 16, 122, 22)
GUICtrlSetFont(-1, 11, 800, 0, "Arial")
$Label2 = GUICtrlCreateLabel("Uninstall Command:", 27, 61, 144, 22)
GUICtrlSetFont(-1, 11, 800, 0, "Arial")
$Button1 = GUICtrlCreateButton("Refresh", 16, 104, 75, 49)
$Input2 = GUICtrlCreateInput("", 176, 64, 281, 21)
GUICtrlSetFont(-1, 1, 400, 0, "MS Sans Serif")
$Button3 = GUICtrlCreateButton("Remove", 384, 104, 73, 49)
$Combo1 = GUICtrlCreateCombo("", 152, 16, 193, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
$Button2 = GUICtrlCreateButton("Get Uninstall String", 360, 16, 105, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Func _ListComputers()
   Local $rArray, $Buff, $Pid = Run(@ComSpec & ' /c net view', '', @SW_HIDE, 6)
   While Not @error
      $buff &= StdoutRead($Pid)
   WEnd
   $rArray = StringRegExp($Buff, "[]+([w-]+)", 3)
   If @Error Then $rArray = StringRegExp(@ComputerName, "(.+)", 1)
   Return $rArray
EndFunc
$aComputers = _ListComputers()
If IsArray($aComputers) Then
    $sData = ""
    For $i = 0 To Ubound($aComputers) -1
        $sData &= $aComputers[$i] & "|"
    Next
Else
Endif
GuiCtrlSetData($Combo1,StringTrimRight($sData, 1), $aComputers[1])

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit

  Case $Button2
   $Name = GUICtrlRead($Combo1)
   $sRegKey = "" & $Name &"HKLMSOFTWAREMicrosoftWindowsCurrentVersionUninstall"
   $iKey = 1
   While 1
    $sHold = RegEnumKey($sRegKey, $iKey)
    If @Error Then ExitLoop
    $iKey += 1
    IF RegRead($sRegKey & $sHold, "DisplayName") = 'Symantec Endpoint Protection' Then
     $sCur_Val = RegRead($sRegKey & $sHold, "UninstallString")
     If NOT @Error Then
     GUICtrlSetData($Input2,$sCur_Val)
     ExitLoop
       EndIf
    EndIf
    WEnd
EndSwitch
WEnd

This code is for a tool to pull the unistall string from a remote computer, but I wanted to have it query the network using "net view" to bring up online computers.

This is the part that errors

test.au3 (40) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
GuiCtrlSetData($Combo1,StringTrimRight($sData, 1), $aComputers[1])
GuiCtrlSetData($Combo1,StringTrimRight($sData, 1), ^ ERROR

However, if I change it to

GuiCtrlSetData($Combo1,StringTrimRight($sData, 1), $aComputers[0])

it pulls my computer name only.

Edited by BryanVest
Link to comment
Share on other sites

  • Moderators

BryanVest,

The returned array starts at the [0] element (like all AutoIt arrays) - if you only have a single computer on the net you only get the [0] element returned. :)

So change the line to read:

$aComputers = _ListComputers()
If IsArray($aComputers) Then
    $sData = ""
    For $i = 0 To UBound($aComputers) - 1
        $sData &= $aComputers[$i] & "|"
    Next
Else
    MsgBox(0, "Error", "No computers found")
    Exit
EndIf
GUICtrlSetData($Combo1, StringTrimRight($sData, 1), $aComputers[0]) ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

All clear? :)

And I can get it to work many times without problem, so I have no idea why you cannot. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

BryanVest,

Delighted I could help. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I know the question has been answered but as of late of been learning regular expressions with the guidance of a couple of people.

With your _ListComputers function I would have changed it a little to always return an array regardless of whether there was an error of not. As you can see I tweaked it as well to have the 0th index return the number of items in the array, just like StringSplit and _FileListToArray.

#include <Array.au3>
#include <Constants.au3>

Local $aArray = _ListComputers()
_ArrayDisplay($aArray)

; Returns an array with the 0th index being the total count.
Func _ListComputers()
    Local $sData = _RunStdOutRead('net view', @SystemDir)
    Local $aArray = StringRegExp('ListComputers-CountIndex' & @CRLF & $sData, '([w-]*)', 3) ; Parse data as well as adding a dummy computer to store the total count.
    If @error Then ; If an error occurs then return an array with just the @ComputerName.
        Local $aError[2] = [1, @ComputerName]
        Return SetError(1, 0, $aError) ; Set @error to show that an error occurred.
    EndIf
    $aArray[0] = UBound($aArray, 1) - 1 ; Find the size of the array and change the 0th item to the total size minus 1.
    Return $aArray
EndFunc   ;==>_ListComputers

Func _RunStdOutRead($sCommand, $sWorkingDirectory = @SystemDir)
    Local $iPID = Run(@ComSpec & ' /c ' & $sCommand, $sWorkingDirectory, @SW_HIDE, $STDOUT_CHILD + $STDERR_CHILD), $sOutput = ''
    While 1
        $sOutput &= StdoutRead($iPID)
        If @error Then
            ExitLoop
        EndIf
    WEnd
    Return $sOutput
EndFunc   ;==>_RunStdOutRead

Local $aComputers = _ListComputers()
Local $sData = ''
For $i = 1 To $aComputers[0]
    $sData &= $aComputers[$i] & '|'
Next
GUICtrlSetData($hCombo, StringTrimRight($sData, 1), $aComputers[1])

Sorry Melba23!

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

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