Sign in to follow this  
Followers 0
Almerto

Need help with a "variable variable"... Not solving with Eval

5 posts in this topic

Hello everybody...

It's not long i've been using autoit language and trying scripting my app i've ran into a problem that i'cant resolve:

i need to create a variable that gets a different name depending on the number of variables that have already been created...

To explain better i'll make an example:

Global $troopstot=2

$troop_1 = GUICtrlCreateLabel("troop_1",10,10)         ;Creates the first group of troop's data
$troop_1L2 = GUICtrlCreateInput("troop_1_2",10,10)
$troop_1L3 = GUICtrlCreateInput("troop_1_3",10,10)
$troop_1L4 = GUICtrlCreateInput("troop_1_4",10,10)

$troop_2 = GUICtrlCreateLabel("troop_2",20,10)         ;Creates the 2nd group of troop's data
$troop_2L2 = GUICtrlCreateInput("troop_2_2",20,10)
$troop_2L3 = GUICtrlCreateInput("troop_2_3",20,10)
$troop_2L4 = GUICtrlCreateInput("troop_2_4",20,10)


GUICtrlSetOnEvent($Button, "AddTroop")                     ;On button pressed event adds a group of troop data to the pre-existing ones (incremental)

Func AddTroop()
$troop_x = GUICtrlCreateLabel("troop_x",20,10)           ;"x" here should be substituted with "&$troopstot" but this would cause an error
$troop_xL2 = GUICtrlCreateInput("troop_x_2",20,10)
$troop_xL3 = GUICtrlCreateInput("troop_x_3",20,10)
$troop_xL4 = GUICtrlCreateInput("troop_x_4",20,10)

$troopstot=($troopstot+1)                                            ;this would grant that the next time we press the button the "$troopstot" will be greater by one from the previous group
EndFunc

The problem is that i can't use "Eval" because these input's could be re-edited during the use of the program and "Eval" would give them all the same variable name, causing all new input's data to be the same...

(at least i think so http://www.autoitscript.com/forum/style_im...cons/icon4.gif)

I hope that my goal is clear and that some good soul will help me out!

Many thanks

Alberto

Share this post


Link to post
Share on other sites



You could use Assign() to make such variable, or you could use arrays.


[center]It's a question of mind over matter, if I don't mind, it doesn't matter.[/center]

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

You could use Assign() to make such variable, or you could use arrays.

I think that the problem still remains, because the, to recall te variable i must use Eval....

Global $troopstot=2

$troop_1 = GUICtrlCreateLabel("troop_1",10,10)       ;Creates the first group of troop's data
$troop_1L2 = GUICtrlCreateInput("troop_1_2",10,10)
$troop_1L3 = GUICtrlCreateInput("troop_1_3",10,10)
$troop_1L4 = GUICtrlCreateInput("troop_1_4",10,10)

$troop_2 = GUICtrlCreateLabel("troop_2",20,10)       ;Creates the 2nd group of troop's data
$troop_2L2 = GUICtrlCreateInput("troop_2_2",20,10)
$troop_2L3 = GUICtrlCreateInput("troop_2_3",20,10)
$troop_2L4 = GUICtrlCreateInput("troop_2_4",20,10)


GUICtrlSetOnEvent($Button, "AddTroop")                   ;On button pressed event adds a group of troop data to the pre-existing ones (incremental)

Func AddTroop()
Assign("troop_"&$troopstot,GUICtrlCreateLabel("troop_x",20,10))                             ;"x" here should be substituted with "&$troopstot" but this would cause an error
Assign("troop_"&$troopstot&"L2",GUICtrlCreateLabel("troop_"&$troopstot&"_2,20,10)) 
Assign("troop_"&$troopstot&"L3",GUICtrlCreateLabel("troop_"&$troopstot&"_2,20,10)) 
Assign("troop_"&$troopstot&"L4",GUICtrlCreateLabel("troop_"&$troopstot&"_2,20,10)) 
$troopstot=($troopstot+1)                                                                           ;this would grant that the next time we press the button the "$troopstot" (...)
EndFunc                                                                                                                                         ;(...)will be greater by one from the previous group

Then with this script i dunno how to read the variables (for instance if we have created let's say 10 of them)

Edited by Almerto

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Use Eval() to read the variables.

Eval("$x = $" & $name);Example

TBH this is not quite a good idea, but I can't find a better alternative except using arrays.

Like this.

Global $troopstot=2
Global $troop[$troopstot][4]
$troop[0][0] = GUICtrlCreateLabel("troop_1",10,10);Creates the first group of troop's data
$troop[0][1] = GUICtrlCreateInput("troop_1_2",10,10)
$troop[0][2] = GUICtrlCreateInput("troop_1_3",10,10)
$troop[0][3] = GUICtrlCreateInput("troop_1_4",10,10)

$troop[1][0] = GUICtrlCreateLabel("troop_2",20,10);Creates the 2nd group of troop's data
$troop[1][1] = GUICtrlCreateInput("troop_2_2",20,10)
$troop[1][2] = GUICtrlCreateInput("troop_2_3",20,10)
$troop[1][3] = GUICtrlCreateInput("troop_2_4",20,10)

GUICtrlSetOnEvent($Button, "AddTroop")          ;On button pressed event adds a group of troop data to the pre-existing ones (incremental)

Func AddTroop()
$troopstot += 1
ReDim $troop[$troopstot][4]
$troop[$troopstot][0] = GUICtrlCreateLabel("troop_2",20,10)
$troop[$troopstot][1] = GUICtrlCreateInput("troop_2_2",20,10)
$troop[$troopstot][2] = GUICtrlCreateInput("troop_2_3",20,10)
$troop[$troopstot][3] = GUICtrlCreateInput("troop_2_4",20,10)
EndFunc
Edited by system24

[center]It's a question of mind over matter, if I don't mind, it doesn't matter.[/center]

Share this post


Link to post
Share on other sites

Use Eval() to read the variables.

Eval("$x = $" & $name);Example

TBH this is not quite a good idea, but I can't find a better alternative except using arrays.

Thanks man, i'll try that... Even if arrays makes me feel a little dazed :)

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