Jump to content
Sign in to follow this  
Hawkysoft

String to call Var

Recommended Posts

Hawkysoft

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

Share this post


Link to post
Share on other sites
Edano

probably you look for Eval()


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

Share this post


Link to post
Share on other sites
Hawkysoft

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

Share this post


Link to post
Share on other sites
Edano

Assign(). please use the helpfile ;)


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

Share this post


Link to post
Share on other sites
Hawkysoft

thanks allot mate, well the problem for me is I'm coming from a different language(ahk) and some things are really confusing for me, I tempt to search the wrong side ;)

Share this post


Link to post
Share on other sites
kylomas

HawkySoft,

You may want to take a closer look at the Help file.

If Eval($tString)  = 1 Then

This is NOT how you use eval.

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

Share this post


Link to post
Share on other sites
Hawkysoft

@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

Share this post


Link to post
Share on other sites
Edano

HawkySoft,

You may want to take a closer look at the Help file.

If Eval($tString)  = 1 Then

This is NOT how you use eval.

kylomas


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

Share this post


Link to post
Share on other sites
Hawkysoft

 

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

Share this post


Link to post
Share on other sites
Edano

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]

Share this post


Link to post
Share on other sites
Hawkysoft
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

Share this post


Link to post
Share on other sites
Edano

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]

Share this post


Link to post
Share on other sites
kylomas

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

Share this post


Link to post
Share on other sites
Hawkysoft

thanks for your effort....

Share this post


Link to post
Share on other sites
kylomas

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

Share this post


Link to post
Share on other sites
Hawkysoft

@kylomas, thank you very much, well I needed it since I got multiple on/off buttons this is how I done it, and it's working flawless

----
Edited by Hawkysoft

Share this post


Link to post
Share on other sites
kylomas

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

Share this post


Link to post
Share on other sites
Hawkysoft

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

Share this post


Link to post
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
Sign in to follow this  

×