Jump to content

Get a constant name by its Value


Juvigy
 Share

Recommended Posts

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

  • Moderators

Juvigy,

An associated array would be helpful here - nutster has an excellent one here. :x

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

M23

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

  • Moderators

trancexx,

It would have been most unlike you not to "dare" to say it! :P

Could I enquire what you might suggest to solve the problem? :x

M23

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

  • Moderators

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

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

M23

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

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 by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

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

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