Sign in to follow this  
Followers 0
Automan Empire

Can I Put A Variable In A Variable Name?

3 posts in this topic

Help! I write "monkey code".

I have written a script that does what I want it to do, but the code is ugly. I am looking for a way to it more compact and easy to maintain.

#include <GuiConstants.au3>
;my "monkey code" that calculates some values, and assigns them to 
Dim $parameterx1, $parameterx2,  $parameterx3,  $parameterx4,  $parameterx5,  $parameterx6,  $parameterx7,  $parameterx8,  $parameterx9
Dim $parametery1, $parametery2,  $parametery3,  $parametery4,  $parametery5,  $parametery6,  $parametery7,  $parametery8,  $parametery9
Dim $type1, $type2, $type3, $type4, $type5, $type6, $type7, $type8, $type9

GUICreate("Name of GUI",1000,200, 100,200,-1,)
$listview = GUICtrlCreateListView ("xxxx       |TYPE  |xxxxx    |xxxxx|xxx|xxx|xxxx|xxxx|xxxxxxx    |xxxx      |xxxx       |xxxxx     |xxxxxxxxxxx                  |xxxxxx |",10,10,980,180);,)

;creates list view items
$item1=GUICtrlCreateListViewItem("|||||||||||||",$listview)
$item2=GUICtrlCreateListViewItem("|||||||||||||",$listview)
$item3=GUICtrlCreateListViewItem("|||||||||||||",$listview)
$item4=GUICtrlCreateListViewItem("|||||||||||||",$listview)
$item5=GUICtrlCreateListViewItem("|||||||||||||",$listview)
$item6=GUICtrlCreateListViewItem("|||||||||||||",$listview)
$item7=GUICtrlCreateListViewItem("|||||||||||||",$listview)
$item8=GUICtrlCreateListViewItem("|||||||||||||",$listview)
$item9=GUICtrlCreateListViewItem("|||||||||||||",$listview)

GUISetState()

assignvalues();this is just an example of values that would be calculated and assigned
determinetype() 
;other functions()

Do
  $msg = GUIGetMsg ()
Until $msg = $GUI_EVENT_CLOSE
Exit

Func assignvalues()
;values would be calculated and assigned to variables in this function..........
;these are just examples:
$parameterx1=25
$parameterx2=32
$parameterx3=30
$parameterx4=31
$parameterx5=29
$parameterx6=28
$parameterx7=35
$parameterx8=38
$parameterx9=25
$parametery1=6
$parametery2=9
$parametery3=7 
$parametery4=8 
$parametery5=3 
$parametery6=10 
$parametery7=5
$parametery8=7 
$parametery9=8
EndFunc

Func determinetype()
;*******************this is the ugly part that I would like to learn how to optimize*******************
if $parameterX1 < 30 and $parameterY1 < 7 then $type1 = "Good"
    if $parameterX1 < 30 and $parameterY1 >= 7 then $type1 = "Fair"
        if $parameterX1 >= 30 and $parameterY1 < 7 then $type1 = "Horrible"
            if $parameterX1 >= 30 and $parameterY1 >= 7 then $type1 = "Bad"
GUICtrlSetData(4,"|"&$type1&"||||||||||||",)

if $parameterX2 < 30 and $parameterY2 < 7 then $type2 = "Good"
    if $parameterX2 < 30 and $parameterY2 >= 7 then $type2 = "Fair"
        if $parameterX2 >= 30 and $parameterY2 < 7 then $type2 = "Horrible"
            if $parameterX2 >= 30 and $parameterY2 >= 7 then $type2 = "Bad"
GUICtrlSetData(5,"|"&$type2&"||||||||||||",)

if $parameterX3 < 30 and $parameterY3 < 7 then $type3 = "Good"
    if $parameterX3 < 30 and $parameterY3 >= 7 then $type3 = "Fair"
        if $parameterX3 >= 30 and $parameterY3 < 7 then $type3 = "Horrible"
            if $parameterX3 >= 30 and $parameterY3 >= 7 then $type3 = "Bad"
GUICtrlSetData(6,"|"&$type3&"||||||||||||",)

if $parameterX4 < 30 and $parameterY4 < 7 then $type4 = "Good"
    if $parameterX4 < 30 and $parameterY4 >= 7 then $type4 = "Fair"
        if $parameterX4 >= 30 and $parameterY4 < 7 then $type4 = "Horrible"
            if $parameterX4 >= 30 and $parameterY4 >= 7 then $type4 = "Bad"
GUICtrlSetData(7,"|"&$type4&"||||||||||||",)

if $parameterX5 < 30 and $parameterY5 < 7 then $type5 = "Good"
    if $parameterX5 < 30 and $parameterY5 >= 7 then $type5 = "Fair"
        if $parameterX5 >= 30 and $parameterY5 < 7 then $type5 = "Horrible"
            if $parameterX5 >= 30 and $parameterY5 >= 7 then $type5 = "Bad"
GUICtrlSetData(8,"|"&$type5&"||||||||||||",)

if $parameterX6 < 30 and $parameterY6 < 7 then $type6 = "Good"
    if $parameterX6 < 30 and $parameterY6 >= 7 then $type6 = "Fair"
        if $parameterX6 >= 30 and $parameterY6 < 7 then $type6 = "Horrible"
            if $parameterX6 >= 30 and $parameterY6 >= 7 then $type6 = "Bad"
GUICtrlSetData(9,"|"&$type6&"||||||||||||",)

if $parameterX7 < 30 and $parameterY7 < 7 then $type7 = "Good"
    if $parameterX7 < 30 and $parameterY7 >= 7 then $type7 = "Fair"
        if $parameterX7 >= 30 and $parameterY7 < 7 then $type7 = "Horrible"
            if $parameterX7 >= 30 and $parameterY7 >= 7 then $type7 = "Bad"
GUICtrlSetData(10,"|"&$type7&"||||||||||||",)

if $parameterX8 < 30 and $parameterY8 < 7 then $type8 = "Good"
    if $parameterX8 < 30 and $parameterY8 >= 7 then $type8 = "Fair"
        if $parameterX8 >= 30 and $parameterY8 < 7 then $type8 = "Horrible"
            if $parameterX8 >= 30 and $parameterY8 >= 7 then $type8 = "Bad"
GUICtrlSetData(11,"|"&$type8&"||||||||||||",)

if $parameterX9 < 30 and $parameterY9 < 7 then $type9 = "Good"
    if $parameterX9 < 30 and $parameterY9 >= 7 then $type9 = "Fair"
        if $parameterX9 >= 30 and $parameterY9 < 7 then $type9 = "Horrible"
            if $parameterX9 >= 30 and $parameterY9 >= 7 then $type9 = "Bad"
GUICtrlSetData(12,"|"&$type9&"||||||||||||",)
EndFunc

what I would like is some sort of compact function similar to this:

Func neaterdeterminetype()
    for $number = 1 to 9
if $parameterx($number) < 30 and $parametery($number) < 7 then $type($number) = "Good"
    if $parameterx($number) < 30 and $parametery($number) >= 7 then $type($number) = "Fair"
        if $parameterx($number) >= 30 and $parametery($number) < 7 then $type($number) = "Horrible"
            if $parameterx($number) >= 30 and $parametery($number) >= 7 then $type($number) = "Bad"
GUICtrlSetData(4,"|"&$type($number)&"||||||||||||",)
Next
EndFunc

Is there a way to do something like this to make the code less spaghetti-like? It may require something like putting a variable in a variable name, if that is possible.


"I've seen your work in the past, and it's novice at best..." SmOke_N

Share this post


Link to post
Share on other sites



Your "Variable in a Variable" idea is interestingly the exact same one I had a while back.. the solution that I was given was to use arrays... It works out pretty well for what you want to do as well.

#include <GuiConstants.au3>
;my "monkey code" that calculates some values, and assigns them to
Dim $parameterx[9]
Dim $parametery[9]
Dim $type[9]

GUICreate("Name of GUI",1000,200)
$listview = GUICtrlCreateListView ("xxxx       |TYPE  |xxxxx    |xxxxx|xxx|xxx|xxxx|xxxx|xxxxxxx    |xxxx      |xxxx       |xxxxx     |xxxxxxxxxxx                  |xxxxxx |",10,10,980,180);,)

;creates list view items
$item1=GUICtrlCreateListViewItem("|||||||||||||",$listview)
$item2=GUICtrlCreateListViewItem("|||||||||||||",$listview)
$item3=GUICtrlCreateListViewItem("|||||||||||||",$listview)
$item4=GUICtrlCreateListViewItem("|||||||||||||",$listview)
$item5=GUICtrlCreateListViewItem("|||||||||||||",$listview)
$item6=GUICtrlCreateListViewItem("|||||||||||||",$listview)
$item7=GUICtrlCreateListViewItem("|||||||||||||",$listview)
$item8=GUICtrlCreateListViewItem("|||||||||||||",$listview)
$item9=GUICtrlCreateListViewItem("|||||||||||||",$listview)

GUISetState()

assignvalues();this is just an example of values that would be calculated and assigned
determinetype()
;other functions()

Do
  $msg = GUIGetMsg ()
Until $msg = $GUI_EVENT_CLOSE

Func assignvalues()
;values would be calculated and assigned to variables in this function..........
;these are just examples:
$parameterx[0]=25
$parameterx[1]=32
$parameterx[2]=30
$parameterx[3]=31
$parameterx[4]=29
$parameterx[5]=28
$parameterx[6]=35
$parameterx[7]=38
$parameterx[8]=25
$parametery[0]=6
$parametery[1]=9
$parametery[2]=7
$parametery[3]=8
$parametery[4]=3
$parametery[5]=10
$parametery[6]=5
$parametery[7]=7
$parametery[8]=8
EndFunc

Func determinetype()
For $x = 1 To Ubound ($parameterx) - 1
if $parameterX[$x] < 30 and $parameterY[$x] < 7 then $type[$x] = "Good"
    if $parameterX[$x] < 30 and $parameterY[$x] >= 7 then $type[$x] = "Fair"
        if $parameterX[$x] >= 30 and $parameterY[$x] < 7 then $type[$x] = "Horrible"
            if $parameterX[$x] >= 30 and $parameterY[$x] >= 7 then $type[$x] = "Bad"
GUICtrlSetData($x + 3,"|"&$type[$x]&"||||||||||||")
Next
EndFunc

Share this post


Link to post
Share on other sites

Thank you very much. That is a great idea! :)


"I've seen your work in the past, and it's novice at best..." SmOke_N

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