RegularGuy Posted February 7, 2011 Share Posted February 7, 2011 Hi, I need help writing this script so that I don't need to write so many repetitive lines. I'm sorry if this has been answered previously, I've already spent several hours looking and trying things, but I just don't know what to look for anymore. I'm still quite new to this all. Thanks in advance for you help expandcollapse popup#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <SliderConstants.au3> #include <Array.au3> #include <EditConstants.au3> #include <ButtonConstants.au3> $loop = 10 ;======================THIS IS HOW TO CONTROL THE NUMBER OF LOOPS=================== Global $Guiarr[$loop][2] Global $numarr[$loop] = ["Zero", "One", "Two","Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"] $x = 0 $y = 30 GUICreate ("Gui Loop", 300, 300) Do $Guiarr[$x][0] = GUICtrlCreateCheckbox($numarr[$x], 30, $y, 75, 20) $Guiarr[$x][1] = GUICtrlCreateEdit("0", 110, $y, 50, 20, 0x2000) GuiCtrlSetState (-1, $GUI_DISABLE) $x = $x + 1 $y = $y + 20 Until $x = $loop GUISetState () While 1 $msg = GUIGetMsg() Switch $msg Case $Guiarr[0][0] _Enable ($Guiarr[0][0],$Guiarr[0][1]) Case $Guiarr[1][0] _Enable ($Guiarr[1][0],$Guiarr[1][1]) Case $Guiarr[2][0] _Enable ($Guiarr[2][0],$Guiarr[2][1]) Case $Guiarr[3][0] _Enable ($Guiarr[3][0],$Guiarr[3][1]) Case $Guiarr[4][0] _Enable ($Guiarr[4][0],$Guiarr[4][1]) Case $Guiarr[5][0] _Enable ($Guiarr[5][0],$Guiarr[5][1]) Case $Guiarr[6][0] _Enable ($Guiarr[6][0],$Guiarr[6][1]) Case $Guiarr[7][0] _Enable ($Guiarr[7][0],$Guiarr[7][1]) Case $Guiarr[8][0] _Enable ($Guiarr[8][0],$Guiarr[8][1]) Case $Guiarr[9][0] _Enable ($Guiarr[9][0],$Guiarr[9][1]) Case -3 Exit EndSwitch WEnd Func _Enable ($check, $edit) IF GUICtrlRead($check) == 1 Then GUICtrlSetstate ($edit, $GUI_ENABLE) Else GUICtrlSetstate ($edit, $GUI_DISABLE) EndIf EndFunc Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 7, 2011 Moderators Share Posted February 7, 2011 RegularGuy,Switch cannot read an array, but you can inside a Case - like this: While 1 $msg = GUIGetMsg() Switch $msg Case -3 Exit Case Else For $i = 0 To $loop - 1 If $msg = $Guiarr[$i][0] Then _Enable ($Guiarr[$i][0],$Guiarr[$i][1]) Next EndSwitch WEndAll clear? M23 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...
RegularGuy Posted February 7, 2011 Author Share Posted February 7, 2011 Melba23, Fantastic!!! That's Exactly what I was looking for. Interesting that you used "Case Else". Not that I want to do this but just more for curiosity's sake; How would you then call 2 different arrays that needed to run two different Functions? So if we take my original example and then add another group of Check boxes that when checked would do something else. I hope that makes sense. I guess my question is, can you read an array without using "Case Else"? Thank you so much for you help!! Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 7, 2011 Moderators Share Posted February 7, 2011 RegularGuy, How would you then call 2 different arrays that needed to run two different Functions?We would need use a little trick. AutoIt allocates ControlIDs sequentially if you create the controls in IMMEDIATE succession. So we do just that and then use a Case to look for the range of IDs: expandcollapse popup#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <SliderConstants.au3> #include <Array.au3> #include <EditConstants.au3> #include <ButtonConstants.au3> $loop = 10 ;======================THIS IS HOW TO CONTROL THE NUMBER OF LOOPS=================== Global $Guiarr_1[$loop][2], $Guiarr_2[$loop][2] Global $numarr[$loop] = ["Zero", "One", "Two","Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"] GUICreate ("Gui Loop", 600, 300) ; Create a range of checkboes $y = 30 For $i = 0 To $loop - 1 $Guiarr_1[$i][0] = GUICtrlCreateCheckbox($numarr[$i], 30, $y, 75, 20) $y = $y + 20 Next ; Create another range of checkboxes $y = 30 For $i = 0 To $loop - 1 $Guiarr_2[$i][0] = GUICtrlCreateCheckbox($numarr[$i], 330, $y, 75, 20) $y = $y + 20 Next ; Create other controls $y = 30 For $i = 0 To $loop - 1 $Guiarr_1[$i][1] = GUICtrlCreateInput("0", 110, $y, 50, 20, 0x2000) GuiCtrlSetState (-1, $GUI_DISABLE) $Guiarr_2[$i][1] = GUICtrlCreateLabel("", 410, $y, 50, 20) GUICtrlSetBkColor(-1, 0x00FF00) GuiCtrlSetState (-1, $GUI_DISABLE) $y = $y + 20 Next GUISetState () While 1 $msg = GUIGetMsg() Switch $msg Case -3 Exit Case $Guiarr_1[0][0] To $Guiarr_1[$loop - 1][0] ; Is it in the first range For $i = 0 To $loop - 1 If $msg = $Guiarr_1[$i][0] Then _Enable ($Guiarr_1[$i][0],$Guiarr_1[$i][1]) Next Case $Guiarr_2[0][0] To $Guiarr_2[$loop - 1][0] ; Is it in the second range For $i = 0 To $loop - 1 If $msg = $Guiarr_2[$i][0] Then _Colour ($Guiarr_2[$i][0],$Guiarr_2[$i][1]) Next EndSwitch WEnd Func _Enable ($check, $edit) IF GUICtrlRead($check) = 1 Then GUICtrlSetstate ($edit, $GUI_ENABLE) Else GUICtrlSetstate ($edit, $GUI_DISABLE) EndIf EndFunc Func _Colour ($check, $label) IF GUICtrlRead($check) = 1 Then GUICtrlSetBkColor ($label, 0xFF0000) Else GUICtrlSetBkColor ($label, 0x00FF00) EndIf EndFunc It takes a bit longer to set up because you need to create the checkboxes in separate loops, but it makes the While...WEnd loop much easier. All clear? M23 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...
RegularGuy Posted February 8, 2011 Author Share Posted February 8, 2011 Melba23, That was brilliant! That answers all my questions. Thank you So much for you help. I didn't realize that you could write "Case $Guiarr_1[0][0] To $Guiarr_1[$loop - 1][0]" That helps so much. Thanks again Link to comment Share on other sites More sharing options...
senatin Posted January 26, 2023 Share Posted January 26, 2023 sorry for reopening this Thread But Why This 2 sample return different Result? my script will have more cases and more values inside array and I think its more neat and easy to construct it with switch rather than forloop. But I'm still open if there's a better way thank you in advance. Local $test[2] = [2, 0] Switch 2 Case $test[0] To $test[Ubound($test)-1] ConsoleWrite(@CR&"WALK"&@CR) Case Else ConsoleWrite(@CR&"ELSE"&@CR) EndSwitch vr Local $test[2] = [0, 2] Switch 2 Case $test[0] To $test[1] ConsoleWrite(@CR&"WALK"&@CR) Case Else ConsoleWrite(@CR&"ELSE"&@CR) EndSwitch Link to comment Share on other sites More sharing options...
AutoBert Posted January 26, 2023 Share Posted January 26, 2023 1 hour ago, senatin said: But Why This 2 sample return different Result? Use the same creating order from values and both script will have the same result. senatin 1 Link to comment Share on other sites More sharing options...
senatin Posted January 26, 2023 Share Posted January 26, 2023 (edited) Thank you I now Get it I thought its working but its actually not Supposedly it says Else but still give me Walk #include <Array.au3> Local $test[] _ArrayAdd($test, 1) _ArrayAdd($test, 1) Switch 2 Case $test[0] To $test[1] ConsoleWrite(@CR&"WALK"&@CR) Case Else ConsoleWrite(@CR&"ELSE"&@CR) EndSwitch but not this Local $test[2] $test[0] = 2 $test[1] = 1 Switch 2 Case $test[0] To $test[1] ConsoleWrite(@CR&"WALK"&@CR) Case Else ConsoleWrite(@CR&"ELSE"&@CR) EndSwitch Can you give me sample ty Edited January 26, 2023 by senatin Link to comment Share on other sites More sharing options...
pixelsearch Posted January 27, 2023 Share Posted January 27, 2023 (edited) @senatin the syntax you used in your last post isn't correct because it creates a Map variable, not an Array variable : Local $test[] If you had checked @error just after _ArrayAdd, or if you had tried to display the "Array" with _ArrayDisplay (it won't have displayed anything), then you would have noticed something was going wrong. Based on your last post, here are 2 tests that say Else : #include <Array.au3> ; Local $test[] ; AutoIt 3.3.14.5 : fatal error ("Variable subscript badly formatted") ; ; AutoIt 3.3.16.1 : VarGetType($test) will be a Map, not an Array Local $test[0] ; AutoIt 3.3.14.5 and AutoIt 3.3.16.1 are ok ; VarGetType($test) will be an Array in both environments. ; _ArrayAdd($test, 1) ; why add 1 when it should be 2 ? _ArrayAdd($test, 2) _ArrayAdd($test, 1) _ArrayDisplay($test, "1st test") Switch 2 Case $test[0] To $test[1] ConsoleWrite(@CR&"WALK"&@CR) Case Else ConsoleWrite(@CR&"ELSE"&@CR) EndSwitch #include <Array.au3> Local $test[2] $test[0] = 2 $test[1] = 1 _ArrayDisplay($test, "2nd test") Switch 2 Case $test[0] To $test[1] ConsoleWrite(@CR&"WALK"&@CR) Case Else ConsoleWrite(@CR&"ELSE"&@CR) EndSwitch Now, concerning the script found in your 1st post : Local $test[2] = [2, 0] Switch 2 ; Case $test[0] To $test[Ubound($test)-1] ; e.g. 2 To 0 Case 2 To 0 ; not effectued at all, because descending ConsoleWrite(@CR&"WALK"&@CR) Case Else ; <=== THIS WILL BE THE RESULT ConsoleWrite(@CR&"ELSE"&@CR) EndSwitch It seems that "Case 2 To 0" never matches a "Switch 2" because the values found in the Case are descending. The following script just indicates what happens comparatively in a For...Next loop, when the values are descending (especially the resulting $i = 2 in 2nd example and not -10 : even if the loop isn't processed, the value of $i becomes 2) $i = - 10 For $i = 0 To 2 Next ConsoleWrite("$i = " & $i & @crlf) ; 3 $i = - 10 For $i = 2 To 0 ; wrong syntax, just to show $i = 2 after the loop, and not -10 Next ConsoleWrite("$i = " & $i & @crlf) ; 2 $i = - 10 For $i = 2 To 0 Step - 1 ; correct syntax Next ConsoleWrite("$i = " & $i & @crlf) ; - 1 Edited January 27, 2023 by pixelsearch Additional infos senatin 1 Link to comment Share on other sites More sharing options...
senatin Posted January 27, 2023 Share Posted January 27, 2023 9 hours ago, pixelsearch said: @senatin the syntax you used in your last post isn't correct because it creates a Map variable, not an Array variable : Local $test[] If you had checked @error just after _ArrayAdd, or if you had tried to display the "Array" with _ArrayDisplay (it won't have displayed anything), then you would have noticed something was going wrong. Based on your last post, here are 2 tests that say Else : #include <Array.au3> ; Local $test[] ; AutoIt 3.3.14.5 : fatal error ("Variable subscript badly formatted") ; ; AutoIt 3.3.16.1 : VarGetType($test) will be a Map, not an Array Local $test[0] ; AutoIt 3.3.14.5 and AutoIt 3.3.16.1 are ok ; VarGetType($test) will be an Array in both environments. ; _ArrayAdd($test, 1) ; why add 1 when it should be 2 ? _ArrayAdd($test, 2) _ArrayAdd($test, 1) _ArrayDisplay($test, "1st test") Switch 2 Case $test[0] To $test[1] ConsoleWrite(@CR&"WALK"&@CR) Case Else ConsoleWrite(@CR&"ELSE"&@CR) EndSwitch #include <Array.au3> Local $test[2] $test[0] = 2 $test[1] = 1 _ArrayDisplay($test, "2nd test") Switch 2 Case $test[0] To $test[1] ConsoleWrite(@CR&"WALK"&@CR) Case Else ConsoleWrite(@CR&"ELSE"&@CR) EndSwitch Now, concerning the script found in your 1st post : Local $test[2] = [2, 0] Switch 2 ; Case $test[0] To $test[Ubound($test)-1] ; e.g. 2 To 0 Case 2 To 0 ; not effectued at all, because descending ConsoleWrite(@CR&"WALK"&@CR) Case Else ; <=== THIS WILL BE THE RESULT ConsoleWrite(@CR&"ELSE"&@CR) EndSwitch It seems that "Case 2 To 0" never matches a "Switch 2" because the values found in the Case are descending. The following script just indicates what happens comparatively in a For...Next loop, when the values are descending (especially the resulting $i = 2 in 2nd example and not -10 : even if the loop isn't processed, the value of $i becomes 2) $i = - 10 For $i = 0 To 2 Next ConsoleWrite("$i = " & $i & @crlf) ; 3 $i = - 10 For $i = 2 To 0 ; wrong syntax, just to show $i = 2 after the loop, and not -10 Next ConsoleWrite("$i = " & $i & @crlf) ; 2 $i = - 10 For $i = 2 To 0 Step - 1 ; correct syntax Next ConsoleWrite("$i = " & $i & @crlf) ; - 1 Thank You I Finally See Whats wrong on my Thought. Its actually the range of Values inside array thats being use and not the range of Arrays. so my script now has change Local $test[3][4] = [[3, 0, 5, 2],[1,4,7],[6,8]] For $i=0 To UBound($test, 2)-1 Select Case 2 = $test[0][$i] ConsoleWrite(@CR&"Its in Test[0]"&@CR) ExitLoop Case 2 = $test[1][$i] ConsoleWrite(@CR&"Its in Test[1]"&@CR) ExitLoop Case 2 = $test[2][$i] ConsoleWrite(@CR&"Its in Test[2]"&@CR) ExitLoop EndSelect Next so switch isnt actually the one I needed its Select. Thanks Link to comment Share on other sites More sharing options...
jugador Posted February 4, 2023 Share Posted February 4, 2023 (edited) @senatin using your above code (2D Array) = ( If / Switch / Select / Do...Until / While...WEnd / Recursion ) method expandcollapse popup#include <Array.au3> Local $Search_Value = 2 Local $test[3][4] = [[3, 0, 5, 2],[1,4,7],[6,8]] _ArrayDisplay($test) ConsoleWrite('> test 1 (using If)' & @crlf) For $i = 0 To UBound($test, 1)-1 For $k = 0 To UBound($test, 2)-1 If $test[$i][$k] = $Search_Value Then ConsoleWrite('$test[' & $i & '][' & $k & ']' & @crlf) ExitLoop 2 EndIf Next Next ConsoleWrite('> test 2 (using Switch)' & @crlf) For $i = 0 To UBound($test, 1)-1 For $k = 0 To UBound($test, 2)-1 Switch $test[$i][$k] Case $Search_Value ConsoleWrite('$test[' & $i & '][' & $k & ']' & @crlf) ExitLoop 2 Case Else EndSwitch Next Next ConsoleWrite('> test 3 (using Select)' & @crlf) For $i = 0 To UBound($test, 1)-1 For $k = 0 To UBound($test, 2)-1 Select Case $test[$i][$k] = $Search_Value ConsoleWrite('$test[' & $i & '][' & $k & ']' & @crlf) ExitLoop 2 Case Else EndSelect Next Next ConsoleWrite('> test 4 (Do Until)' & @crlf) Local $i = 0 Local $k = 0 Local $row = UBound($test, 1) Local $col = UBound($test, 2) Do If $test[$i][$k] = $Search_Value Then ConsoleWrite('$test[' & $i & '][' & $k & ']' & @crlf) ExitLoop EndIf $k = $k + 1 If $k = $col Then $i = $i + 1 $k = 0 EndIf Until $i = $row ConsoleWrite('> test 5 (While Loop)' & @crlf) Local $i = 0 Local $k = 0 Local $row = UBound($test, 1) Local $col = UBound($test, 2) While 1 If ($i = $row) Then ExitLoop If ($test[$i][$k] = $Search_Value) Then ConsoleWrite('$test[' & $i & '][' & $k & ']' & @crlf) ExitLoop EndIf $k = $k + 1 If $k = $col Then $i = $i + 1 $k = 0 EndIf WEnd ConsoleWrite('> test 6 (Recursion Method)' & @crlf) __Recursion_Method($test, 0, 0, $Search_Value) Func __Recursion_Method(ByRef $oArray, $i = 0, $k = 0, $oValue = 0) Local $row = UBound($oArray, 1) Local $col = UBound($oArray, 2) - 1 If ($i = $row) Then Return If $oArray[$i][$k] = $oValue Then ConsoleWrite('$test[' & $i & '][' & $k & ']' & @crlf) Return EndIf If $k = $col Then $i = $i + 1 $k = 0 Else $k += 1 EndIf __Recursion_Method($oArray, $i, $k, $oValue) EndFunc Edited February 6, 2023 by jugador senatin 1 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