Jump to content

why "Error: Subscript used with non-Array Variable" ?!


Recommended Posts

Sometimes I do not understand Autoit...

Why does it give "Error: Subscript used with non-Array Variable" on the switch instruction of the following simple script?

Dim array1D[1]
array1D[0] = ""
array1D = StringRegExp("one two three","ten|eleven",3)
switch array1D[0]
    case "ten"
        msgbox(0,"","ten")
    case "eleven"
        msgbox(0,"","eleven")
    case else
        msgbox(0,"","else...")
endswitch
if UBound($array1D) > 1 then msg(0,"","more than 1 match found")
Link to comment
Share on other sites

wait, all your array1D need to be $array1D, it is a variable, and needs the $

also, if you dim an array, you can't just assign $array1D a value, you have to specify $array1D[$i] where $i is the array item #

Dim $array1D[2]
$array1D[0] = ""
$array1D[1] = StringRegExp("one two three","ten|eleven",3)
switch $array1D[0]
    case "ten"
        msgbox(0,"","ten")
    case "eleven"
        msgbox(0,"","eleven")
    case else
        msgbox(0,"","else...")
endswitch
if UBound($array1D[1]) > 1 then msg(0,"","more than 1 match found")
Edited by dufran3
Link to comment
Share on other sites

  • Moderators

Imbuter2000,

The SRE produces no matches and so $array1D (I assume the missing $ is a typo) is not an array. :>

You need to check after a call to an SRE with option 3 that you do indeed get an array - IsArray is a good place to start, or else check @error. ;)

M23

Edit: Bonsoir wakillon! :unsure:

Edited by Melba23

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

It should fail long before that since you didn't declare anything as a variable by using "$". This isn't vbs you are working with, it's Autoit.

You asked StringRegExp() to return an array and because that SRE is totally incorrect it returned an error code instead of the array.

Always error check. With a flag > 0 you have 2 methods

$array1D = StringRegExp("one two three","ten|eleven",3)
;;If @Error Then _DoSomething() ;; Using the @Error method
If IsArray($array1D) Then _DoSomethingElse() ;; check for array method which is prefered for people new to AutoIt array code.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Ah, thank you all for the great support.

I wrote the wrong script by memory and forgot something. The script of my first message should be the following, and I still don't understand why that array is not CREATED by the first two lines, that I added right to compensate the fact that the stringregexp fails. Can you say why?

Dim $array1D[1]
$array1D[0] = ""
$array1D = StringRegExp("one two three","ten|eleven",3)
switch $array1D[0]
    case "ten"
        msgbox(0,"","ten")
    case "eleven"
        msgbox(0,"","eleven")
    case else
        msgbox(0,"","else...")
endswitch
if UBound($array1D) > 1 then msgbox(0,"","more than 1 match found")
Link to comment
Share on other sites

You are still totally incorrect. That RegEx can not possibly match and you have to check for an error after the RegEx OR check to see if an array was created.

;;Dim $array1D[1]  You don't need this at all
;;$array1D[0] = ""  Don't use it
$array1D = StringRegExp("one two three","ten|eleven",3)
If NOT @Error Then
switch $array1D[0]
    case "ten"
        msgbox(0,"","ten")
    case "eleven"
        msgbox(0,"","eleven")
    case else
        msgbox(0,"","else...")
endswitch
EndIf
;;if UBound($array1D) > 1 then msgbox(0,"","more than 1 match found")

OR

;;Dim $array1D[1]  You don't need this at all
;;$array1D[0] = ""  Don't use it
$array1D = StringRegExp("one two three","ten|eleven",3)
If IsArray($array1D) Then
switch $array1D[0]
    case "ten"
        msgbox(0,"","ten")
    case "eleven"
        msgbox(0,"","eleven")
    case else
        msgbox(0,"","else...")
endswitch
EndIf
;;if UBound($array1D) > 1 then msgbox(0,"","more than 1 match found")

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

why do you say that I cannot use $array1D[0] = "" or $array1D[0] = "foobar" to create the array(?) before the stringregexp?

does the stringregexp (with flag 3 for global matches) DELETEs an existant array if it doesn't find matches?

Link to comment
Share on other sites

StringRegExp() with flag 3 will create a 0 based array if there is a match or return an empty string if it fails.

You can use something like

Global $aArray

with no dimensions; otherwise that array will be overwritten anyway. Of course none of this matters until you get an actual working SRE anyway.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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