Jump to content

Yet Another "incorrect number of subscripts or subscript"


Recommended Posts

I know it's simple but after two days of searching and trying, I have to admit defeat.

Examples of displaying an array in a ListBox usually create the array from a for...next loop. Here, I load the array from a _FileReadToArray function, create a GUI window, a ListView, check if I actually have an array with _ArrayDisplay, then pass it off to _GUICtrlListView_AddArray. Works fine but when I reach the _GUICtrlListView_AddArray, I get the infamous "Array variable has incorrect number of subscripts or subscript dimension range exceeded." There is some tiny thing I'm overlooking but just can't get what it is.

Thanks in advance,

Bill

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.6.1

 Script Function:
    Trying to load a file into an array, then into a ListBox
    Title:  Heavily modified Melba's Array to Listbox routine.

#ce ----------------------------------------------------------------------------

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <GuiListView.au3>
#include <file.au3>
#include <array.au3>


Local $aArray, $hListView, $hGui

; Read file into an array.
; tfm01.txt is a 10 line text file created in Notepad that says: "One, two, three" etc.
If Not _FileReadToArray("tfm01.txt",$aArray) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf

; Create GUI Window
$hGUI = GUICreate(" Test of Load File into Listbox", 500, 410)

; Create listview
$hListView = GUICtrlCreateListView("File Array", 2, 2, 490, 400)

_GUICtrlListView_SetItemCount($hListView, $aArray[0])

; Show we have an array loaded and the number of elements
_ArrayDisplay ($aArray)

; Put array into ListView
_GUICtrlListView_AddArray($hListView, $aArray)

;==== ...THEN THE GUI ELEMENTS ARE DISPLAYED WITH THIS LINE.
GUISetState()

MsgBox (1, "","")
Link to comment
Share on other sites

As written, the _GUICtrlListView_AddArray requires a 2D array, a 1D array causes that error. The documentation for the function doesn't mention that though. I ran into this problem myself a little while ago and found out the hard way.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

  • Moderators

BillAlso,

Welcome to the AutoIt forum. :)

There is a nasty little surprise hidden in the _GUICtrlListView_AddArray function - which is not mine by the way. :D

You need to declare the array with 2 dimensions - even if (as in this case) you only have a single column. I have not investigated, but I would guess this is no doubt to do with the internal workings of the UDF - I remember having to put in lots of checks in my GUIListViewEx UDF so that I did not have the same limitation. :)

So you need to transfer your elements from the 1D array you get from _FileReadToArray into a 2D array to pass to the UDF. Here is how you might do it - look for the <<<<<<<<<<<<<< lines:

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.6.1

 Script Function:
    Trying to load a file into an array, then into a ListBox
    Title:  Heavily modified Melba's Array to Listbox routine.

#ce ----------------------------------------------------------------------------

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <GuiListView.au3>
#include <file.au3>
#include <array.au3>


Local $aArray, $hListView, $hGui

; Read file into an array.
; tfm01.txt is a 10 line text file created in Notepad that says: "One, two, three" etc.
If Not _FileReadToArray("tfm01.txt",$aArray) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf

; Create GUI Window
$hGUI = GUICreate(" Test of Load File into Listbox", 500, 410)

; Create listview
$hListView = GUICtrlCreateListView("File Array", 2, 2, 490, 400)

_GUICtrlListView_SetItemCount($hListView, $aArray[0])

; Show we have an array loaded and the number of elements
_ArrayDisplay ($aArray)

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Global $aNewArray[$aArray[0]][1]
For $i = 1 To $aArray[0]
    $aNewArray[$i - 1][0] = $aArray[$i]
Next

_ArrayDisplay($aNewArray)
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

; Put array into ListView
_GUICtrlListView_AddArray($hListView, $aNewArray) ; <<<<<<<<<<<<<<<<<
;==== ...THEN THE GUI ELEMENTS ARE DISPLAYED WITH THIS LINE.
GUISetState()

MsgBox (1, "","")

It is interesting to see that there is no visual difference between the 2 arrays in _ArrayDisplay even though one is 1D and the other is 2D. However, declaring single-column 2D arrays is a useful trick from time to time. ;)

All clear? :P

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

BrewMan and Melba, thanks much for saving me from "the home". Really thought I was loosing it, there. And, Melba, thanks for the workaround; that really nailed what is going on.

So: does this constitute a bug in _GUICtrlListView_AddArray or is that a feature, as in being necessary for the control to work properly in other cases?

Bill

Link to comment
Share on other sites

As Microsoft would explain it, it's an undocumented feature. I believe it's because the UDF function is expecting sub-items in the array and wasn't designed to handle a listview with a single column.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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