Jump to content
Sign in to follow this  
donaldduck

a simple variable question [Solved]

Recommended Posts

donaldduck

If I wanted the value of a variable in the name of another variable in php I would say:

$x = 'swamp';
$y = 'atchafalaya';
$z = 'location';

$%x%_%y%_%z% = 'in LA';

echo $swamp_atchafalaya_location;
//output would be 'in LA'

how the hell can i do this with autoit? I have tried eval, execute, and the %$var% methods and just have no idea.

Edited by donaldduck

Share this post


Link to post
Share on other sites
Authenticity

Opt("ExpandVarStrings", 1)

$x = "swamp"
$y = "atchafalaya"
$z = "location"

Assign("$x$_$y$_$z$", "in LA")

ConsoleWrite(Eval("$x$_$y$_$z$") & @CRLF)
;~ output would be 'in LA'

The Opt("ExpandVarStrings", 1) line is there so you don't need to use annoying superfluous string concatenation operation.

Share this post


Link to post
Share on other sites
donaldduck

Opt("ExpandVarStrings", 1)

$x = "swamp"
$y = "atchafalaya"
$z = "location"

Assign("$x$_$y$_$z$", "in LA")

ConsoleWrite(Eval("$x$_$y$_$z$") & @CRLF)
;~ output would be 'in LA'

The Opt("ExpandVarStrings", 1) line is there so you don't need to use annoying superfluous string concatenation operation.

Thanks for the reply, but your solution does not solve my problem. I need to be able to access the data via $swamp_atchafalaya_location, not $x$_$y$_$z$. As I recall this is accomplished in several other languages via forcing variable evaluation in the %var% manor. i.e.

$a = 'sigh';

$%$a% = 'apple';

echo $sigh;
//prints 'apple';

I understand that the example is overcomplicated, but the example is a simplification of a larger issue. If I can get my question answered on the small scale I should be able to translate it.

Share this post


Link to post
Share on other sites
donaldduck

more specifics, maybe the autoit wizards know how to do this easier. My code works just fine except where I want to redefine variables for easier debugging later down the road.

$ini_names = IniReadSectionNames ( "test.ini" )

If @error Then
    MsgBox(0, "error", "Cannot open .ini")
    Exit
EndIf

For $i = 1 To $ini_names[0]
    $section = IniReadSection ( "test.ini", $ini_names[$i] )
        For $ii = 1 To $section[0][0]
            $pos_%$ini_names[$i]%_%$section[$ii][0]% = $section[$ii][1]
        Next
Next


Exit

My .ini:

[sec1]
x=23
y=108
desc=random

[sec2]
x=44
y=548
desc=random2

I want to end up being able to call $pos_sec2_x and it work be working fine, but I do not know what (if there is a way) of having autoit evaluate a variable's value on the left side of an expression. AHK does it just fine, but I don't want to bridge everything I have already written over to it.

Edited by donaldduck

Share this post


Link to post
Share on other sites
Authenticity

Opt("ExpandVarStrings", 1)

$a = 'sigh'

Assign('a', 'apple')
ConsoleWrite($a & @CRLF)

$x = 'swamp';
$y = 'atchafalaya';
$z = 'location';

Assign('$x$_$y$_$z$', 'in LA')
ConsoleWrite($swamp_atchafalaya_location & @CRLF)

AU3Check will warn you that $swamp_atchafalaya_location is references but is not initialized. It should work, though.

Edit: Even though, I don't see the point in writing such a code. If you know the content of $x, $y and $z, you don't need such a code.

Edited by Authenticity

Share this post


Link to post
Share on other sites
donaldduck

cool, let me double check.

As it stands now the only way I have gotten it to work is by doing this:

Global $pos_sec1_x = IniRead("test.ini", "sec1", "x", "NotFound")
Global $pos_sec1_y = IniRead("test.ini", "sec1", "y", "NotFound")
Global $pos_sec1_desc = IniRead("test.ini", "sec1", "desc", "NotFound")

Global $pos_sec2_x = IniRead("test.ini", "sec2", "x", "NotFound")
Global $pos_sec2_y = IniRead("test.ini", "sec2", "y", "NotFound")
Global $pos_sec2_desc = IniRead("test.ini", "sec2", "desc", "NotFound")

The point is that I may add 100 more sections to the config file and I would rather not have to keep expanding my declaration section when a for loop would do.

Share this post


Link to post
Share on other sites
Thatsgreat2345

I'm curious as to the point of having named variables like this?

Share this post


Link to post
Share on other sites
donaldduck

I'm curious as to the point of having named variables like this?

it make it 100% easier for me to debug...

Share this post


Link to post
Share on other sites
Thatsgreat2345

No offense but there is most likely a much more efficient, and better way of doing this besides making your life easier to debug.

Share this post


Link to post
Share on other sites
donaldduck

No offense but there is most likely a much more efficient, and better way of doing this besides making your life easier to debug.

Most likely... but I can edit the x/y locations quickly via:

$ini_names = IniReadSectionNames ( "test.ini" )

If @error Then
    MsgBox(0, "error", "Cannot open .ini")
    Exit
EndIf

$confirm = MsgBox(1, "confirm", "View data?" )
If $confirm = 1 Then
    For $i = 1 To $ini_names[0]
        $section = IniReadSection ( "test.ini", $ini_names[$i] )
        MsgBox(4096, $ini_names[$i], " " & $section[1][0] & ": " & $section[1][1] & @CRLF & $section[2][0] & ": " & $section[2][1] & @CRLF & $section[3][0] & ": " & $section[3][1] & @CRLF)
    Next
EndIf

$confirm = MsgBox(1, "confirm", "Reprogram?" )
If $confirm = 1 Then
    For $i = 1 To $ini_names[0]
        $section = IniReadSection ( "test.ini", $ini_names[$i] )
        While 1
            Sleep ( 150 )
            ToolTip("right click on " & $section[3][1], $tt_x, $tt_y)
            If _IsPressed("02") Then
                $pos = MouseGetPos()
                ToolTip("Got it!", $tt_x, $tt_y)
                IniWrite("test.ini", $ini_names[$i], $section[1][0], $pos[0])
                IniWrite("test.ini", $ini_names[$i], $section[2][0], $pos[1])
                ExitLoop
            EndIf
        WEnd
    Next
EndIf

Thus adding or editing points ad nauseum is easy.. if I could only get the problem to work forward as well it would be nice.

Share this post


Link to post
Share on other sites
Tvern

A small tweak to Authenticity's code will stop the anoying warnings.

Opt("ExpandVarStrings", 1)

$x = "swamp"
$y = "atchafalaya"
$z = "location"

Assign('$x$_$y$_$z , 'in LA')
ConsoleWrite(Eval("swamp_atchafalaya_location") & @CRLF)

Share this post


Link to post
Share on other sites
donaldduck

A small tweak to Authenticity's code will stop the anoying warnings.

Opt("ExpandVarStrings", 1)

$x = "swamp"
$y = "atchafalaya"
$z = "location"

Assign('$x$_$y$_$z , 'in LA')
ConsoleWrite(Eval("swamp_atchafalaya_location") & @CRLF)

solution works great! Thanks both of you.

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  

×