Loc Posted December 30, 2021 Posted December 30, 2021 For example, I set the variables to different names to make it easier to remember, instead of setting a variable $Key[nn], it's easy to call. Is there a way to solve the case of arrays containing variables to make the code less verbose? #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <Array.au3> ;~ Global $arrayall[3] = [$Label1, $Label2, $Label3, $Label4] #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 529, 437, 192, 124) $Label1 = GUICtrlCreateLabel("", 56, 24, 36, 17) $Label2 = GUICtrlCreateLabel("", 56, 64, 36, 17) $Label3 = GUICtrlCreateLabel("", 56, 104, 36, 17) $Label4 = GUICtrlCreateLabel("", 56, 144, 36, 17) $Button1 = GUICtrlCreateLabel("SET", 56, 144, 36, 17) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### Global $arrayall[3] = [$Label1, $Label2, $Label3, $Label4] While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 _SETDATA() EndSwitch WEnd Func _SETDATA() For $i = 0 To UBound $arrayall[3] GUICtrlSetData($arrayall[$i], 1) Next EndFunc
Luke94 Posted December 30, 2021 Posted December 30, 2021 Your code works (with a couple of fixes): #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <Array.au3> ;~ Global $arrayall[3] = [$Label1, $Label2, $Label3, $Label4] #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 529, 437, 192, 124) $Label1 = GUICtrlCreateLabel("", 56, 24, 50, 17) $Label2 = GUICtrlCreateLabel("", 56, 64, 50, 17) $Label3 = GUICtrlCreateLabel("", 56, 104, 50, 17) $Label4 = GUICtrlCreateLabel("", 56, 144, 50, 17) $Button1 = GUICtrlCreateButton("SET", 56, 184, 36, 17) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### Global $arrayall[4] = [$Label1, $Label2, $Label3, $Label4] While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 _SETDATA() EndSwitch WEnd Func _SETDATA() For $i = 0 To (UBound($arrayall) - 1) GUICtrlSetData($arrayall[$i], 'Set data') Next EndFunc You could also declare an array of labels at the top, then assign them as you create the labels. Example: #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <Array.au3> ;~ Global $arrayall[3] = [$Label1, $Label2, $Label3, $Label4] Global $g_iLabelID[4] #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 529, 437, 192, 124) $g_iLabelID[0] = GUICtrlCreateLabel("", 56, 24, 50, 17) $g_iLabelID[1] = GUICtrlCreateLabel("", 56, 64, 50, 17) $g_iLabelID[2] = GUICtrlCreateLabel("", 56, 104, 50, 17) $g_iLabelID[3] = GUICtrlCreateLabel("", 56, 144, 50, 17) $Button1 = GUICtrlCreateButton("SET", 56, 184, 36, 17) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### ;~ Global $arrayall[4] = [$Label1, $Label2, $Label3, $Label4] While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 _SETDATA() EndSwitch WEnd Func _SETDATA() For $i = 0 To (UBound($g_iLabelID) - 1) GUICtrlSetData($g_iLabelID[$i], 'Set data') Next EndFunc Apologies if I'm misunderstanding your question..
Loc Posted December 30, 2021 Author Posted December 30, 2021 Did I mention if the variable is set to different names and not $Key[nn]
Danp2 Posted December 30, 2021 Posted December 30, 2021 It isn't clear to me what you are trying to accomplish, so you may want to restate the problem. Latest Webdriver UDF Release Webdriver Wiki FAQs
Loc Posted December 30, 2021 Author Posted December 30, 2021 (edited) 4 minutes ago, Danp2 said: It isn't clear to me what you are trying to accomplish, so you may want to restate the problem. Example: I set 5 variables. $A, $B, $C, $D, $E, ... Then how can I combine these variables into an array so that I can change the state or data quickly without having to be verbose. Because the variable name I set to make myself easy to remember and recognize. Edited December 30, 2021 by Loc
Solution Luke94 Posted December 30, 2021 Solution Posted December 30, 2021 I may still be misunderstanding so apologies if so. Maybe a Map? Maybe an Enum? expandcollapse popup#include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <Array.au3> ;~ Global $arrayall[3] = [$Label1, $Label2, $Label3, $Label4] Global Enum $LABEL_1 = 0, $LABEL_2, $LABEL_3, $LABEL_4 Global $g_iLabelID[4] #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 529, 437, 192, 124) $Label1 = GUICtrlCreateLabel("", 56, 24, 100, 17) $Label2 = GUICtrlCreateLabel("", 56, 64, 100, 17) $Label3 = GUICtrlCreateLabel("", 56, 104, 100, 17) $Label4 = GUICtrlCreateLabel("", 56, 144, 100, 17) $Button1 = GUICtrlCreateButton("SET", 56, 184, 36, 17) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### Global $arrayall[4] = [$Label1, $Label2, $Label3, $Label4] While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 _SETDATA() EndSwitch WEnd Func _SETDATA() For $i = 0 To (UBound($arrayall) - 1) GUICtrlSetData($arrayall[$i], 'Set data') Next GUICtrlSetData($arrayall[$LABEL_3], 'This is Label 3') EndFunc argumentum 1
Danp2 Posted December 30, 2021 Posted December 30, 2021 I would suggest reviewing this thread for ideas -- Latest Webdriver UDF Release Webdriver Wiki FAQs
junkew Posted December 30, 2021 Posted December 30, 2021 The question/details are not completely clear to me You seem to want some logic name for each control You seem to have a need to iterate over an array of named items Then you probably should look at the assign, eval, execute, assign functions https://www.autoitscript.com/autoit3/docs/functions/Assign.htm And its good to know that a variable can be a function reference $a=10 $b=20 $c=30 $d=40 $e=50 $arrItemsOfInterest=stringsplit("a,c,e",",") For $i = 0 To (UBound($arrItemsOfInterest) - 1) assign($arrItemsOfInterest[$i], eval($arrItemsOfInterest[$i])+5) Next consolewrite($a & @CRLF) consolewrite($b & @CRLF) consolewrite($c & @CRLF) consolewrite($d & @CRLF) consolewrite($e & @CRLF) So what you want you can accomplish with $arrItemsOfInterest=stringsplit("label1,label2,label3,label4",",") FAQ 31 How to click some elements, FAQ 40 Test automation with AutoIt, Multithreading CLR .NET Powershell CMDLets
ad777 Posted December 30, 2021 Posted December 30, 2021 (edited) $A, $B, $C, $D, $E, ... Then how can I combine these variables into an array so that I can change the state, data expandcollapse popup#include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <Array.au3> ;~ Global $arrayall[3] = [$Label1, $Label2, $Label3, $Label4] Global Enum $LABEL_1 = 0, $LABEL_2, $LABEL_3, $LABEL_4 Global $g_iLabelID[4] #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 529, 437, 192, 124) $Label1 = GUICtrlCreateLabel("", 56, 24, 100, 17) $Label2 = GUICtrlCreateLabel("", 56, 64, 100, 17) $Label3 = GUICtrlCreateLabel("", 56, 104, 100, 17) $Label4 = GUICtrlCreateLabel("", 56, 144, 100, 17) $Button1 = GUICtrlCreateButton("SET", 56, 184, 36, 17) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### Global $arrayall[4] = ["1", "2", "3", "4"] While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 _SETDATA('off',"ee") EndSwitch WEnd Func _SETDATA($state,$data) For $i = 0 To (UBound($arrayall) - 1) Local $var $var &= $arrayall[$i] Next Local $prev = $var Local $new_array[1] = $var if $state = "on" Then $new_array = $data MsgBox(0,'',$new_array) Else $new_array = $prev MsgBox(0,'',$new_array) EndIf EndFunc Edited December 30, 2021 by ad777 none
markyrocks Posted December 31, 2021 Posted December 31, 2021 (edited) Edit Nm I just can't. Edited December 31, 2021 by markyrocks Spoiler "I Believe array math to be potentially fatal, I may be dying from array math poisoning"
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