Imbuter2000 Posted May 10, 2011 Share Posted May 10, 2011 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 More sharing options...
dufran3 Posted May 10, 2011 Share Posted May 10, 2011 (edited) 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 May 10, 2011 by dufran3 Link to comment Share on other sites More sharing options...
wakillon Posted May 10, 2011 Share Posted May 10, 2011 because StringRegExp ( "one two three","ten|eleven", 3 ) return @error = 1 so array1d is now a string, not an array... AutoIt 3.3.14.2 X86 - SciTE 3.6.0 - WIN 8.1 X64 - Other Example Scripts Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted May 10, 2011 Moderators Share Posted May 10, 2011 (edited) 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. M23Edit: Bonsoir wakillon! Edited May 10, 2011 by Melba23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
GEOSoft Posted May 10, 2011 Share Posted May 10, 2011 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 More sharing options...
dufran3 Posted May 10, 2011 Share Posted May 10, 2011 Melba23, my new goal is to scour the forum and try and help someone with a correct answer before you get there...it seems I always fail...haha Link to comment Share on other sites More sharing options...
wakillon Posted May 10, 2011 Share Posted May 10, 2011 (edited) @Melba23Bonsoir a vous tres cher !Au fait, avais tu parié sur le jaune ? Edited May 10, 2011 by wakillon AutoIt 3.3.14.2 X86 - SciTE 3.6.0 - WIN 8.1 X64 - Other Example Scripts Link to comment Share on other sites More sharing options...
Imbuter2000 Posted May 11, 2011 Author Share Posted May 11, 2011 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 More sharing options...
Imbuter2000 Posted May 11, 2011 Author Share Posted May 11, 2011 (edited) even if I add the line if @error = 1 then $array_sgr[0] = "abc" the switch still fails with that damned error "Subscript used with non-Array variable.: switch $array1D[0]" Edited May 11, 2011 by Imbuter2000 Link to comment Share on other sites More sharing options...
GEOSoft Posted May 11, 2011 Share Posted May 11, 2011 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 More sharing options...
Imbuter2000 Posted May 11, 2011 Author Share Posted May 11, 2011 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 More sharing options...
GEOSoft Posted May 11, 2011 Share Posted May 11, 2011 (edited) 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 likeGlobal $aArraywith no dimensions; otherwise that array will be overwritten anyway. Of course none of this matters until you get an actual working SRE anyway. Edited May 11, 2011 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 More sharing options...
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