Sign in to follow this  
Followers 0
Oroumov

Make A Variable With A Variable ?

15 posts in this topic

Hi all,

Very simple question :

Say I have $values="apples,pears,tomatoes"

I split this on the "," symbol, I get an array, and $array[2]="pears"

How can I create variables named $Var_apples, $Var_pears, etc... ?

Or to make it even more simple :

Say i have $i=20

How do I create 20 variables named $Var1 to $Var20 ?

Thanks for your help !

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hi all,

Very simple question :

Say I have $values="apples,pears,tomatoes"

I split this on the "," symbol, I get an array, and $array[2]="pears"

How can I create variables named $Var_apples, $Var_pears, etc... ?

Or to make it even more simple :

Say i have $i=20

How do I create 20 variables named $Var1 to $Var20 ?

Thanks for your help !

Look up Assign() in the help file. Edited by big_daddy

Share this post


Link to post
Share on other sites

Look up Assign() in the help file.

Problem with that is :

- Situation 1 : OK

Global $Var1

$i=1

If Assign("Var"&$i,"hello") Then MsgBox(0,"Test",$Var1)

- Situation 2 : NOK

$i=1

If Assign("Var"&$i,"hello") Then MsgBox(0,"Test",$Var1)

And if I have to declare all variables as Global at the beginning, then it doesn't help :think:

Do you reproduce this behavior ?

Share this post


Link to post
Share on other sites

It's only a warning, you could just continue and it runs fine.

You don't have to declare as global, you could just Dim or set them to local.


"I have discovered that all human evil comes from this, man's being unable to sit still in a room. " - Blaise Pascal

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

You might want to take a look at my Binary Tree UDF (link in my sig). It lets you associate a value (any variable, even another array) with a key value (you key values would be "pears", "apples", etc)

For your question of creating variables $var1 through $var20 - I think what you want is an array. Search for array in the help file.

Edited by blindwig

Share this post


Link to post
Share on other sites

You might want to take a look at my Binary Tree UDF (link in my sig). It lets you associate a value (any variable, even another array) with a key value (you key values would be "pears", "apples", etc)

For your question of creating variables $var1 through $var20 - I think what you want is an array. Search for array in the help file.

Hi again,

My question has evolved :think:

Say I still have an array, coming from a stringsplit

array[0]=2

array[1]=apples

array[2]=tomatoes

What I want is to create a treeview, main tree branches being apples, tomatoes, etc..., and sub-branches being various info. So Apples is one branch, with two subbranches, Tomatoes another branch with another set of two sub-branches etc etc...

If I hardcode this, it looks simply like this (very easy)

$TreeBranch_Apples = GUICtrlCreateTreeViewItem("Apples",$MainTree)

$TreeBranch_Apples_Info1 = GUICtrlCreateTreeViewItem("Info1",$TreeBranch_Apples)

$TreeBranch_Apples_Info2 = GUICtrlCreateTreeViewItem("Info2",$Treebranch_Apples)

(* Nb Of Items)

Now what I'd like is to create these numerous branches and sub-branches automatically, given the array above.

I've tried this :

Assign("TreeBranch_"&$array[1],GUICtrlCreateTreeViewItem($array[1],$MainTree))

and it works to create the main branch.

But the problem arises while trying to create the sub-branch, because I'm suppose to be referring to the above branch :

Assign("TreeBranch_"&$array[1]&"_Info1",GUICtrlCreateTreeViewItem("Info1",xxx)

But then, what should I write instead of xxx ?

It should be $TreeBranch_Apples, but I can't write it down myself, or the purpose of the automation is lost.

It can't write anything based on "TreeBranch_"&$array[1] either, because it will not be evaluated here.

Then how could I do this ? (I'm ready to hear every option, including these of rewriting the whole code in a much smarter way :()

Thx !

Share this post


Link to post
Share on other sites

Hi again,

My question has evolved :think:

Say I still have an array, coming from a stringsplit

array[0]=2

array[1]=apples

array[2]=tomatoes

What I want is to create a treeview, main tree branches being apples, tomatoes, etc..., and sub-branches being various info. So Apples is one branch, with two subbranches, Tomatoes another branch with another set of two sub-branches etc etc...

If I hardcode this, it looks simply like this (very easy)

$TreeBranch_Apples = GUICtrlCreateTreeViewItem("Apples",$MainTree)

$TreeBranch_Apples_Info1 = GUICtrlCreateTreeViewItem("Info1",$TreeBranch_Apples)

$TreeBranch_Apples_Info2 = GUICtrlCreateTreeViewItem("Info2",$Treebranch_Apples)

(* Nb Of Items)

Now what I'd like is to create these numerous branches and sub-branches automatically, given the array above.

I've tried this :

Assign("TreeBranch_"&$array[1],GUICtrlCreateTreeViewItem($array[1],$MainTree))

and it works to create the main branch.

But the problem arises while trying to create the sub-branch, because I'm suppose to be referring to the above branch :

Assign("TreeBranch_"&$array[1]&"_Info1",GUICtrlCreateTreeViewItem("Info1",xxx)

But then, what should I write instead of xxx ?

It should be $TreeBranch_Apples, but I can't write it down myself, or the purpose of the automation is lost.

It can't write anything based on "TreeBranch_"&$array[1] either, because it will not be evaluated here.

Then how could I do this ? (I'm ready to hear every option, including these of rewriting the whole code in a much smarter way :))

Thx !

I think you are making it too complicated. Just use a simple array to hold the control IDs so you don't have to give each one a text variable name. This is a good training opportunity for me and I'm coding an example to post shortly. :(


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

I think you are making it too complicated. Just use a simple array to hold the control IDs so you don't have to give each one a text variable name. This is a good training opportunity for me and I'm coding an example to post shortly. :(

Completed example using a 2D array to hold values for the TreeViewItems:

; Test variable TreeView
#include <GuiConstants.au3>

$InputString = "Apple, Orange, Kiwi, Banana"

$InputArray = StringSplit($InputString, ",")

; $TreeArray[0][0] = count
;   [n][0] = name
;   [n][1] = item ctrl id
;   [n][2] = subitem1 ctrl id
;   [n][3] = subitem2 ctrl id
Dim $TreeArray[$InputArray[0] + 1][4]
$TreeArray[0][0] = UBound($TreeArray) - 1

$GuiHeight = ($TreeArray[0][0] * 20) + 20
$GuiHandle = GUICreate("Fruit Tree Test", 400, $GuiHeight)
$Label_1 = GUICtrlCreateLabel("The Fruit Tree View Test", 10, ($GuiHeight / 2) - 30, 180, 20)
$Button_1 = GUICtrlCreateButton("DISPLAY", 70, $GuiHeight / 2, 60, 30)

$TreeStyle = BitOR($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS)
$Tree_1 = GUICtrlCreateTreeView(200, 10, 190, $GuiHeight - 20, $TreeStyle, $WS_EX_CLIENTEDGE)
For $n = 1 To $InputArray[0]
    $TreeArray[$n][0] = $InputArray[$n]
    $TreeArray[$n][1] = GUICtrlCreateTreeViewItem($TreeArray[$n][0], $Tree_1)
    $TreeArray[$n][2] = GUICtrlCreateTreeViewItem("Info One", $TreeArray[$n][1])
    $TreeArray[$n][3] = GUICtrlCreateTreeViewItem("Info Two", $TreeArray[$n][1])
Next

GUISetState()

While 1
    $GuiMsg = GUIGetMsg()
    Select
        Case $GuiMsg = $GUI_EVENT_CLOSE
            Exit
        Case $GuiMsg = $Button_1
            $Selection = GUICtrlRead($Tree_1)
            For $n = 1 To $TreeArray[0][0]
                Select
                    Case $TreeArray[$n][1] = $Selection
                        $Selection = $TreeArray[$n][0]
                        ExitLoop
                    Case $TreeArray[$n][2] = $Selection
                        $Selection = $TreeArray[$n][0] & " - Note One"
                        ExitLoop
                    Case $TreeArray[$n][3] = $Selection
                        $Selection = $TreeArray[$n][0] & " - Note Two"
                        ExitLoop
                EndSelect
            Next
            GUICtrlSetData($Label_1, $Selection)
    EndSelect
WEnd

Let me know if that solves the problem, I already learned a lot from doing it... :think:

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Share this post


Link to post
Share on other sites

How is all that any better than just using a binary tree?

It's better because... :)

...I know how to code with arrays and haven't tried a b-tree yet? :(

I did consider creating a universal 2D array where every row contained [<parent>][<item>]. The idea was [0][0] would be a count, and [0][1] = "\" (root string). After that, each row would be like:

[1][0] = "\"          [1][1] = "Branch_1"
   [2][0] = "\"       [2][1] = "Branch_2"
   [3][0] = "Branch_1"   [3][1] = "Branch_3"
   [4][0] = "Branch_2"   [4][1] = "Leaf_1"
   [5][0] = "Branch_2"   [5][1] = "Leaf_2"
   [6][0] = "\"       [6][1] = "Branch_4"
   [7][0] = "Branch_3"   [7][1] = "Leaf_5"
   [8][0] = "Branch_4"   [8][1] = "Leaf_3"
   [9][0] = "Branch_1"   [9][1] = "Leaf_4"

Which would diagram as:

"Branch_3"----"Leaf_5"
         "Branch_1"-----|
         |            "Leaf_4"
         |
         |           "Leaf_3
         "Branch_2"----|
         |           "Leaf_2
"\" -----|
         |
         "Branch_4"----"Leaf_3"

But that's playing with stuff I don't have any experience with yet... please have mercy on poor student! :think:

Besides, the fuctional part that takes the input string and gerates the table (excluding the GUI and gui message loop) are a grand total of 10 lines of code! Would adding a bunch of extra b-tree functions really make this particular task cleaner? ;)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Then how could I do this ? (I'm ready to hear every option, including these of rewriting the whole code in a much smarter way :think:)

OK, going from your post, I'd say a 2d array (AKA table) would be the best way to do this.

But let's step back for a minute and look at the big picture. What exactly is it that you are trying to do? You're trying to represent some information to the user, correct? What information do you have and how does it need to be presented?

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

OK, going from your post, I'd say a 2d array (AKA table) would be the best way to do this.

But let's step back for a minute and look at the big picture. What exactly is it that you are trying to do? You're trying to represent some information to the user, correct? What information do you have and how does it need to be presented?

Errmmm... :(

That would be a question for that "Oroumov" bloke, what posted the nutters problem in the first place. :)

All seriousness aside, I believe he wanted two specific static sub-items (Info1 and Info2, see post #6) under each main item. The main items came from a string split. I have no idea where the originating string came from. In the example I posted for him, I declare it staticly at the begining of the script to allow him to change it easily to see the results (for one thing, the GUI automaticly adjusts in height to match the number of input items). Anyway, it was the first GUI I've done with a TreeView in it, and I don't think it came out so bad... :think:

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Errmmm... :(

That would be a question for that "Oroumov" bloke, what posted the nutters problem in the first place. :)

Yes, that's who I replied to! ;)

And oops, I meant to say "use a 2d array (AKA table) like PsaltyDS showed you" but it looks like the fingers and the brain weren't cooperating on that one!

All seriousness aside, I believe he wanted two specific static sub-items (Info1 and Info2, see post #6) under each main item. The main items came from a string split. I have no idea where the originating string came from. In the example I posted for him, I declare it staticly at the begining of the script to allow him to change it easily to see the results (for one thing, the GUI automaticly adjusts in height to match the number of input items). Anyway, it was the first GUI I've done with a TreeView in it, and I don't think it came out so bad... :think:

What I'm trying to get at is what is he trying to do? First he's assuming that using variables with names he pulls out of strings would be best. Then wants to do some kind of pseduo-arrays, then trying to do pseudo-arrays with pseudo-names. Obvioiusly someone new to programming, so what I'm asking is for him to tell us exactly what he's wanting to so maybe we can show him a better way to do it.

Share this post


Link to post
Share on other sites

Yes, that's who I replied to!

Oops... :">

It's all good, you got me Googling and I found an interesting tutorial on b-trees. Trying to stretch my brain around the basic concepts now. Thanks for the hints. :think:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Yes, that's who I replied to! :)

And oops, I meant to say "use a 2d array (AKA table) like PsaltyDS showed you" but it looks like the fingers and the brain weren't cooperating on that one!

What I'm trying to get at is what is he trying to do? First he's assuming that using variables with names he pulls out of strings would be best. Then wants to do some kind of pseduo-arrays, then trying to do pseudo-arrays with pseudo-names. Obvioiusly someone new to programming, so what I'm asking is for him to tell us exactly what he's wanting to so maybe we can show him a better way to do it.

Thanks for all your help guys, you're right I'm obviously not an expert programmer :(

This is what I was trying to do ("was" because I actually got one solution working)

My input is as follows, it's very simple but then I need something to start with :

A list of elements : "A,B,C" (Will grow in size over time)

For each element, 2 sets of 8 informations "1,2,3,4,5,6,7,8" (these will not grow)

And my presentation requirements are :

For each element, a treebranch. In each treebranch, two subtreebranches, each of them displays one set of 8 informations.

And... that's about it.

The only thing is that 1/ I'm not that good at coding 2/ I haven't been coding for a loooong time and 3/ I tried to start with what I already knew of Autoit, which was the worse thing to do.

Now, given this, and though I have reached a working solution, I'm willing to hear what you guys would have done for this. This will help me for the second version :D

To conclude, here is the code that you are welcome to assassinate ;)

(I've removed all the real content of course, but that's it)

Run it, then please be cruel :D, I realise as I speak that I haven't even tried to use 2D arrays, which would have been much better as I realised in the above scripts, or that my Hide_All and Show_All functions are probably useless as well.

I'm willing to improve :think:

Thx a lot !

#include <GUIConstants.au3>

#region Variables
AutoItSetOption("GUICloseOnESC",1)

$Applications_List = "A:B:C:D" 
$Application_Enum = StringSplit($Applications_List,":")

Dim $Application[($Application_Enum[0]*3)]
Dim $Content[($Application_Enum[0]*3)]
Dim $Data[12]

$Content[1] = "1|2|3|4|5|6|7|8"
$Content[2] = "1|2|3|4|5|6|7|8"

$Content[4] = "1|2|3|4|5|6|7|8"
$Content[5] = "1|2|3|4|5|6|7|8"

$Content[7] = "1|2|3|4|5|6|7|8"
$Content[8] = "1|2|3|4|5|6|7|8"

$Content[10] = "1|2|3|4|5|6|7|8"
$Content[11] = "1|2|3|4|5|6|7|100"

#endregion

#region Interface Graphique
Opt("GUIOnEventMode", 1)
$Interface_Applications = GUICreate("Interface Applications", 492, 273)
GUISetOnEvent($GUI_EVENT_CLOSE, "Exit_Application")

#region Initialisation du Tree Menu

GUICtrlCreateGroup("",2,-4,165,275)
GUICtrlCreateGroup("",171,-4,322,275)

$Applications_Treeview = GUICtrlCreateTreeView(8, 8, 153, 257)

For $i = 0 to (($Application_Enum[0]*3)-1) Step 3
   $Application[$i] = GUICtrlCreateTreeViewItem($Application_Enum[(($i+3)/3)],$Applications_Treeview)
   $Application[$i+1] = GUICtrlCreateTreeViewItem("Docs",$Application[$i])
   GUICtrlSetOnEvent($Application[$i+1], "TreeViewItemSelected")
   $Application[$i+2] = GUICtrlCreateTreeViewItem("Urls",$Application[$i])
   GUICtrlSetOnEvent($Application[$i+2], "TreeViewItemSelected")
   GUICtrlSetImage(-1, "shell32.dll",85,4)
Next 

#endregion Initialisation du Tree Menu

#region Initialisation de la partie droite

$Data[0] = GUICtrlCreateLabel("Titre1", 176, 16, 300, 17)
$Data[1] = GUICtrlCreateLabel("Titre2", 176, 72, 300, 17)
$Data[2] = GUICtrlCreateLabel("Titre3", 176, 128, 300, 17)
$Data[3] = GUICtrlCreateLabel("Titre4", 176, 184, 300, 17)

$Data[4] = GUICtrlCreateInput("Ligne1", 176, 40, 281, 21, -1, $WS_EX_CLIENTEDGE)
$Data[5] = GUICtrlCreateInput("Ligne2", 176, 96, 281, 21, -1, $WS_EX_CLIENTEDGE)
$Data[6] = GUICtrlCreateInput("Ligne3", 176, 152, 281, 21, -1, $WS_EX_CLIENTEDGE)
$Data[7] = GUICtrlCreateInput("Ligne4", 176, 208, 281, 21, -1, $WS_EX_CLIENTEDGE)

$Data[8] = GUICtrlCreateButton("Go", 464, 40, 25, 25)
GUICtrlSetOnEvent($Data[8], "Go")
$Data[9] = GUICtrlCreateButton("Go", 464, 96, 25, 25)
GUICtrlSetOnEvent($Data[9], "Go")
$Data[10] = GUICtrlCreateButton("Go", 464, 152, 25, 25)
GUICtrlSetOnEvent($Data[10], "Go")
$Data[11] = GUICtrlCreateButton("Go", 464, 208, 25, 25)
GUICtrlSetOnEvent($Data[11], "Go")



;~ For $i = 0 to 11
;~  GUICtrlSetState($Data[$i],$GUI_HIDE)
;~ Next

#endregion Initialisation de la partie Droite


#endregion Interface Graphique


#region Corps de la boucle
GUISetState(@SW_SHOW)
;Small trick to expand the tree at the beginning, is there another way ?
Send("{RIGHT}")
For $i = 1 to $Application_Enum[0]-1
    Send ("{DOWN}{DOWN}{DOWN}{RIGHT}")
Next
While 1
    Sleep(10)
WEnd

#endregion Corps de la boucle

Func Exit_application()
    Exit
EndFunc

Func Go()
    For $i = 0 to 3
        If @GUI_CtrlId = $Data[$i+8] Then
        Run("""C:\Program Files\Internet Explorer\IEXPLORE.EXE"" """&GUICtrlRead($Data[$i+4])&"""")     
        EndIf
    Next
EndFunc

Func TreeviewItemSelected()
    For $i = 0 to ($Application_Enum[0]*3)-1
        If @GUI_CtrlId = $Application[$i] Then 
        Genere_Contenu($Content[$i])        
        EndIf
    Next
EndFunc


#region Fonctions
Func Hide_All_Content()
    For $i = 0 to 11
    GUICtrlSetState($Data[$i],$GUI_HIDE)
    Next
EndFunc

Func Show_All_Content()
    For $i = 0 to 11 
    GUICtrlSetState($Data[$i],$GUI_SHOW)
    Next
EndFunc

Func Genere_Contenu($content)
;Hide_All_Content()
    $splitted_content = StringSplit($content,"|")
    GUICtrlSetData($Data[0],$splitted_content[1])
    GUICtrlSetData($Data[1],$splitted_content[3])
    GUICtrlSetData($Data[2],$splitted_content[5])
    GUICtrlSetData($Data[3],$splitted_content[7])
    GUICtrlSetData($Data[4],$splitted_content[2])
    GUICtrlSetData($Data[5],$splitted_content[4])
    GUICtrlSetData($Data[6],$splitted_content[6])
    GUICtrlSetData($Data[7],$splitted_content[8])
;Show_All_Content()
EndFunc
#endregion Fonctions

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