Jump to content

String to call Var


Go to solution Solved by kylomas,

Recommended Posts

I don't know if it's possible, but if it is it would save me loads of code...

what I basically want is this

Local $Test = 1

Func Testing($tString)

  If "$" & $tString  = 1 Then

    .........

  EndIf

EndFunc

 

Anyone got an idea how to do this? So combine the $ and the string together to call the Var (hope it sounds not to confusing ;))

Link to comment
Share on other sites

probably you look for Eval()

 

That helps for the first part yes! thank you, but now how to do it like this?

Func Testing($tString)

  If Eval($tString)  = 1 Then

    Eval($tString) = "blabla"

    .........

  EndIf

EndFunc

So basicaly I wanted to call the Var and not the output from it, hope it makes sense*

Edited by Hawkysoft
Link to comment
Share on other sites

@kylomas, I know mate, it was an example of what I wanted, Edano showed me howto.

Although I've been searching trough the helpfile... any idea how I should do this? or point me towards the right direction?

 

Func Testing($tString)

  If Eval($tString)  = 1 Then

    Assign($String,1)

    GuiCtrlSetData("$" & $tString & "Input","On")

  EndIf

EndFunc

It doesn't error for some reason, but doesn't give what I'm trying to accomplish either..

 

p.s. I tempt to keep all these questions in the same thread since they are all related so the next person who needs it can find it easily

Edited by Hawkysoft
Link to comment
Share on other sites

 

Ah now I see, but don't know what I should use instead than....

Func Testing($tString)

  Local $temp = Eval($tString)

  If $temp  = 1 Then

didnt do the trick either... (I suppose u both mean that I can't use the var that's been set in the function as a string as well...)

Edited by Hawkysoft
Link to comment
Share on other sites

Local $a_b = 12
Local $s = Eval("a" & "_" & "b") ; $s is set to 12

$s = Eval("c") ; $s = "" and @error = 1
Local $sString
If Assign("sString", "Hello") Then MsgBox(4096, "Assign", $sString) ; This will print the text "Hello"

.

it is not very amusing to post the entire help file every night. what more can i do, tell me please ?

Edited by Edano

[color=rgb(255,0,0);][font="'comic sans ms', cursive;"]FukuLeaks[/color][/font]

Link to comment
Share on other sites

Local $a_b = 12
Local $s = Eval("a" & "_" & "b") ; $s is set to 12

$s = Eval("c") ; $s = "" and @error = 1
Local $sString
If Assign("sString", "Hello") Then MsgBox(4096, "Assign", $sString) ; This will print the text "Hello"

.

it is not very amusing to post the entire help file every night. what more can i do, tell me please ?

I'm truly sorry if I annoy the living sh** out of you, but appearantly I don't understand it... I've read the page like 20x already to see what I'm doing wrong...

Local $s = Eval("a" & "_" & "b") ; $s is set to 12

so IMO Local $temp  = Eval($tString)  should work, but appearantly it doesn't and needs to be a pure "string" so my question is how to do it elseway...

p.s. please don't think im some kind of person that doesn't do research... the simple things of autoit i don't get yet... and it's hard to google for stuff you don't know how it's been called in this language.

Edited by Hawkysoft
Link to comment
Share on other sites

I'm truly sorry if I annoy the living sh** out of you, but appearantly I don't understand it... I've read the page like 20x already to see what I'm doing wrong...

Local $s = Eval("a" & "_" & "b") ; $s is set to 12

so IMO Local $temp  = Eval($tString)  should work, but appearantly it doesn't and needs to be a pure "string" so my question is how to do it elseway...

p.s. please don't think im some kind of person that doesn't do research... the simple things of autoit i don't get yet... and it's hard to google for stuff you don't know how it's been called in this language.

.

where do you see an "$" in eval(...) ????????

[color=rgb(255,0,0);][font="'comic sans ms', cursive;"]FukuLeaks[/color][/font]

Link to comment
Share on other sites

  • Solution

See if this helps

local $Test = 1

Testing($Test)

func Testing($tString)

    if eval('tString') = 1 then
        ConsoleWrite('Yes, it equals 1' & @LF)
    endif

endfunc

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Hawkysoft,

Here is a "practical" (word used loosely) example of Assign/Eval.

#include <array.au3>

; array of variable names
local $varTBL[10]

; populate array with strings var0-var9
for $1 = 0 to ubound($varTBL) - 1
    $varTBL[$1] = 'var' & $1
Next

; create variables from array elements and assign 0-9 to the variables
for $1 = 0 to ubound($varTBL) - 1
    assign($varTBL[$1],$1)
next
_arraydisplay($varTBL)

; display the value of each variable
for $1 = 0 to ubound($varTBL) - 1
    ConsoleWrite($varTBL[$1] & ' = ' & Testing($varTBL[$1]) & @LF)
next

; don't let this syntax confuse you.  Eval is using the value of the parameter (a string) as the variable name.
func Testing($tString)
    return eval($tString)
endfunc

However, why would you want to do this?

If it is simply to shorten your script then you are compromising debugging and readability.  The script will probably run slower too.

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Thanks for the explanation.

Future F.Y.I. - It is common to use an array to hold multiple control id's.  I don't know if this applies to your case but it is something to keep in mind. 

Good Luck,

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Thanks for the explanation.

Future F.Y.I. - It is common to use an array to hold multiple control id's.  I don't know if this applies to your case but it is something to keep in mind. 

Good Luck,

kylomas

Yeah I know mate, have done that for several other buttons with more choices than on/off however in this case it seemed for me the best, rlly appreciate your help mate :)

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