Juvigy Posted December 14, 2010 Share Posted December 14, 2010 I have a large list of AUtoIT constants with different values. For example: Const $test1 = 1 Const $test2 = 2 Const $test3 = 3 Is there a way to get a constant name by its value? I mean: Global $check = 2 I want a way to receive "$test2" if the value of $check is 2 or "$test1" if the value of $check is 1 and etc. Hope i explained it well. 10x in advance. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted December 14, 2010 Moderators Share Posted December 14, 2010 Juvigy, An associated array would be helpful here - nutster has an excellent one here. Once you have downloaded it, this script shows how to use it to do what you want: #include <AssocArrays.au3> ; Create an exit mode! HotKeySet("{ESC}", "On_Exit") Func On_Exit() Exit EndFunc ; Create associative array Local $aConst_List AssocArrayCreate($aConst_List, 7) ; Fill array with valuesd and associated names For $i = 0 To 5 AssocArrayAssign($aConst_List, $i, "$test" & $i) Next While 1 ; Generate a value $iRand = Random(0, 5, 1) ; Retrieve the name $sConst = AssocArrayGet($aConst_List,$iRand) ; Display it MsgBox(0, "Result", "Value " & $iRand & " associated with " & $sConst) WEnd Please ask if you need any more pointers. 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...
trancexx Posted December 14, 2010 Share Posted December 14, 2010 But Melba23 that would be terrible use of associative array. Misuse I daresay. ♡♡♡ . eMyvnE Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted December 14, 2010 Moderators Share Posted December 14, 2010 trancexx,It would have been most unlike you not to "dare" to say it! Could I enquire what you might suggest to solve the problem? 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...
trancexx Posted December 14, 2010 Share Posted December 14, 2010 Could I enquire what you might suggest to solve the problem? You could. I suggest not solving it. It's senseless in its bare existence, don't you agree? ♡♡♡ . eMyvnE Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted December 14, 2010 Moderators Share Posted December 14, 2010 trancexx,Like you I have some difficulty in understanding why the OP wants to do what has been asked. But when faced with questions such as this with no real information as to why the problem actually exists, I tend to treat them as existing outside any present reality and to solve them purely as an intellectual exercise. In this case an associative array seemed a reasonable tool. I suppose I could have created an array and used _ArraySearch or any number of other tricks, but that solution took my fancy at that instant - no doubt the result would have been different in another time/space moment. 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...
iamtheky Posted December 14, 2010 Share Posted December 14, 2010 (edited) provided the constants nicely have the matching numbers as in your example #include <array.au3> Dim $Con[4] $Con [0] = "test 0" $Con [1] = "test 1" $Con [2] = "test 2" $Con [3] = "test 3" while 1 $r = _check() msgbox (0, '' , $Con[$r]) Wend func _check () $check = random (0 , 3 , 1) return $check endfunc Edited December 14, 2010 by iamtheky ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
martin Posted December 14, 2010 Share Posted December 14, 2010 (edited) I have a large list of AUtoIT constants with different values. For example: Const $test1 = 1 Const $test2 = 2 Const $test3 = 3 Is there a way to get a constant name by its value? I mean: Global $check = 2 I want a way to receive "$test2" if the value of $check is 2 or "$test1" if the value of $check is 1 and etc. Hope i explained it well. 10x in advance. Maybe you need somethng like this. Dim $names[5] = ["test1","test2","apple5","dog17","tree12"] $check = 3 ConsoleWrite($names[$check] & @CRLF) or like this Func getname($val) Switch $val Case $test1 Return "test1" Case $test2 Return "test2" EndSwitch EndFunc ;==>getname or maybe Const $test1 = 1, $test2 = 2, $apple5 = 3, $dog17 = 17, $tree12 = 12 Dim $names[5]= ["test1", "test2", "apple5", "dog17", "tree12"] $check = 3 ConsoleWrite(NameOf($check) & @CRLF) Func NameOf($val) ConsoleWrite("val = " & $val & @CRLF) For $n = 0 To UBound($names) - 1 If Eval($names[$n]) = $val Then Return "$" & $names[$n] Next Return '' EndFunc ;==>NameOf Edited December 14, 2010 by martin Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script. 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