Jump to content
kartune

Renaming a $var1 to $newvarname

Recommended Posts

Hello, i'm trying to rename a variable to another variable name (not the value attached to the variable).  This would save me TONS of coding.

Here is an example:

 

<autoit>

Local $clear1="$SATJ"  ;sets the $clear1 variable to become $SATJ

GUICtrlSetState($clear1 & "3", $GUI_UNCHECKED)  ;i want this to come out to be $SATJ3, it should look something like - GUICtrlSetState($SATJ3, $GUI_UNCHECKED)

</autoit>

Is this possible?  I've been searching all the forums and help files for hours.  My javascript friend referenced me to the solution on JS, which lead me to searching through the "assign" help on autoit, but not much info on it.

I've already tried
<autoit>

Assign($clear1, $SATJ)

</autoit>

 

any help is greatly appreciated!!! 

Edited by kartune

Share this post


Link to post
Share on other sites

you are only ever assigning values to variables.

How are you confirming that $clear1 & "3" is even a control before you set its state?

Somewhere between the creation of those controls and the setting of their state, I would declare all possible variables; and adjust the values as necessary with case statements.


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

Well i'm trying to avoid hundreds of unnecessary lines of code, i was hoping there'd be an easy way to do this.

This action needs to happen to several different variables under different situtations

;;; This is for the SATJ situation ;;;

GUICtrlSetState($SATJ0, $GUI_UNCHECKED) ;removes radio button selections
GUICtrlSetState($SATJ3, $GUI_UNCHECKED)
GUICtrlSetState($SATJ6, $GUI_UNCHECKED)
GUICtrlSetState($SATJ12, $GUI_UNCHECKED)
GUICtrlSetState($SATJ18, $GUI_UNCHECKED)
GUICtrlSetState($SATJ20, $GUI_UNCHECKED)
GUICtrlSetState($SATJ30, $GUI_UNCHECKED)
GUICtrlSetState($SATJ60, $GUI_UNCHECKED)
GUICtrlSetState($SATJ100, $GUI_UNCHECKED)
GUICtrlSetState($SATJ120, $GUI_UNCHECKED)

 

;;; This is for the OBCD situation ;;;

GUICtrlSetState($OBCD0, $GUI_UNCHECKED) ;removes radio button selections
GUICtrlSetState($OBCD3, $GUI_UNCHECKED)
GUICtrlSetState($OBCD6, $GUI_UNCHECKED)
GUICtrlSetState($OBCD12, $GUI_UNCHECKED)
GUICtrlSetState($OBCD18, $GUI_UNCHECKED)
GUICtrlSetState($OBCD20, $GUI_UNCHECKED)
GUICtrlSetState($OBCD30, $GUI_UNCHECKED)
GUICtrlSetState($OBCD60, $GUI_UNCHECKED)
GUICtrlSetState($OBCD100, $GUI_UNCHECKED)
GUICtrlSetState($OBCD120, $GUI_UNCHECKED)

 

;;; What i would love to have it be ;;;

GUICtrlSetState($changeableVar0, $GUI_UNCHECKED) ;removes radio button selections
GUICtrlSetState($changeableVar3, $GUI_UNCHECKED)
GUICtrlSetState($changeableVar6, $GUI_UNCHECKED)

...

...

...

 

So if i could just rename the variable according to the situation, i could set it to a function and call the same function for every situation.

Hope that makes sense.

Share this post


Link to post
Share on other sites

you can do assignments with variables, if this is headed down your mental path we probably just need to work out where in your script it would best suit, which depends a lot on how you are initially creating the controls and storing IDs as @kylomas inquired.

for $i = 1 to 3
assign("var" & $i , $i)
next

msgbox(0, '' , eval("var1"))
msgbox(0, '' , eval("var2"))
msgbox(0, '' , eval("var3"))

 


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

Hi, Thanks for your responses and sorry i forgot to mention I am not an advanced scripter nor knowledgeable in programming languages.

So i don't exactly understand your questions in regards to storing them as controls or ids.  I'm guessing ID? 

 

What i need is to at any point be able to have autoit rename the variable when stated to.

So if i wanted every code line that contains:  $clear1     to actually become $changedvariable
Everytime the script sees a $clear1 --- it actually executes it as the variable actually being:  $changedvariable           ;NOT $clear1

I apologize this is harder than expected to explain haha.

In my script, i will be alternating back and forth.... so at certain situations, i need:  $clear1     : to actually be

$changedvar1

at other times it will be:     $changedvar2

then others:     $changedvar65
and so on....

I don't want to write out all those lines of code containing:     $clear1          :for everytime i need that specific code for:      $changedvar

I hope this makes sense.  If this works out you will be saving me a lot of copy pasted lines lol.

Edited by kartune
correction grammar

Share this post


Link to post
Share on other sites

kartune,

You have sets of variables containing the control id's of sets of controls that you wish to manipulate as a set without repeating code for each individual variable in each set.

When you create a control that you wish to manipulate you create a corresponding control id.  That control id can be stored in a simple variable, e.g. $myctl = something or in an array, e.g. $checkbox[$1] = something.

The problem you have is program organization, in that you need to manage control id's in sets.  Arrays are perfect for that.  Without example code or more detail I can't really comment in more detail.  I would caution you to stay away from eval/assign if possible.

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
3 minutes ago, kylomas said:

kartune,

You have sets of variables containing the control id's of sets of controls that you wish to manipulate as a set without repeating code for each individual variable in each set.

When you create a control that you wish to manipulate you create a corresponding control id.  That control id can be stored in a simple variable, e.g. $myctl = something or in an array, e.g. $checkbox[$1] = something.

The problem you have is program organization, in that you need to manage control id's in sets.  Arrays are perfect for that.  Without example code or more detail I can't really comment in more detail.  I would caution you to stay away from eval/assign if possible.

Good Luck,

kylomas

thank you very much, that was informative.  I will look into arrays and learn about them!

Share this post


Link to post
Share on other sites

trying to work up a simple example...

Her you go...hope it makes sense, if not just ask...

#include <GUIConstantsEx.au3>
#include <Array.au3>

#AutoIt3Wrapper_Add_Constants=n

Local $gui010 = GUICreate('CHK Box Example', 400, 600)

; arrays to store control ids in, one array per group

Local $chkbox_group1[8]
Local $chkbox_group2[15]
Local $chkbox_group3[4]

; create the controls storing the ctlid in the appropriate array

For $1 = 0 To UBound($chkbox_group1) - 1
    $chkbox_group1[$1] = GUICtrlCreateCheckbox('ChkBox-G1 ' & StringFormat('%02i', $1), 10, $1 * 30 + 10, 100, 20)
Next
For $1 = 0 To UBound($chkbox_group2) - 1
    $chkbox_group2[$1] = GUICtrlCreateCheckbox('ChkBox-G2 ' & StringFormat('%02i', $1), 130, $1 * 30 + 10, 100, 20)
Next
For $1 = 0 To UBound($chkbox_group3) - 1
    $chkbox_group3[$1] = GUICtrlCreateCheckbox('ChkBox-G3 ' & StringFormat('%02i', $1), 260, $1 * 30 + 10, 100, 20)
Next

Local $abuttons[3]

; an array of buttons to clear each group

For $1 = 0 To UBound($abuttons) - 1
    $abuttons[$1] = GUICtrlCreateButton('Clear Group ' & $1 + 1, $1 * 130 + 5, 550, 100, 20)
Next


GUISetState()

Local $msg

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $gui_event_close
            Exit
        Case $abuttons[0]
            _clear(1)
        Case $abuttons[1]
            _clear(2)
        Case $abuttons[2]
            _clear(3)
    EndSwitch
WEnd

Func _clear($group)

    ; semi reasonable use of eval...resolves to $chkbox_group1 or $chkbox_group2 or $chkbox_group3...

    For $1 = 0 To UBound(Eval('chkbox_group' & $group)) - 1
        GUICtrlSetState(Eval('chkbox_group' & $group)[$1], $gui_unchecked)
    Next

EndFunc   ;==>_clear

kylomas

Edited by 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

Thanks!  i'm gonna study this, but just from the looks of it already, i should've started all my code using arrays.

Unfortunately i have so much code already written haha.  

I've learned a lot already, very grateful for your help!

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

  • Similar Content

    • By mlazovjp
      I am going through the AutoIt Wiki and soaking up a lot of great information.  On the “Best Coding Practices” page at https://www.autoitscript.com/wiki/Best_coding_practices#Scopes_of_Variables there seems to be a minor detail missing.  In the “Variable Initialization” section at https://www.autoitscript.com/wiki/Best_coding_practices#Variable_Initialization , there are references to a data type $o, but in the table listing prefixes and their data types, there does not appear to be a prefix “o”.  I assume that this is for Object?
    • By ThePoro
      I have this code 
      It gets variable input from GUI to run a loop and write to a text file then copy it to the clipboard
      But the thing is code runs normally with $px and $py <100 or both > 100 or with $px <10 and $py>100 but It's wrong with 10<$px <100 and $py >100
      Can anyone tell me why?
      Func clone() Local $px = GUICtrlRead($input1) Local $py = GUICtrlRead($input2) Local $temp = "C:\temp.csv" FileDelete($temp) While $px <= $py Local $name = "P"&$px FileWrite($temp, " & $name & ""& @CRLF) $px+= 1 WEnd $a=FileRead($temp) ClipPut($a) MsgBox($MB_ICONINFORMATION,"","Copied to Clipboard") EndFunc  
    • By Skeletor
      Hi Guys,
      Is it possible to get a variable on your For..Next loop? 
       
      Local $Lines1 = _FileCountLines(C:\temp\test.txt) Local $linesToCount2 = $Lines1 + 2 $var = Number($linesToCount2) For $count = 1 To _FileCountLines($FileRead2) Step 1 For $i = $var To $count Next ;Code does stuff here Next Somehow my code doesn't work even though I thought I could convert the variable to a Integer / Number.
      This code I posted above does not move to the next value.
      But the code below does... why is that?
      For $count = 1 To _FileCountLines($FileRead2) Step 1 For $i = 2 To $count Next ;Code does stuff here Next  
      Why is the For loop resetting itself?
      Is it because the program does not cache the variable and needs to keep on acquiring this variable each time?
      If so , how would you make this variable static?

       
    • By Miliardsto
      I got that func
      Func makeHelpImgGUI($title,$width,$height,$img) $img = GUICtrlCreatePic("",20,40,$width,$height) _ResourceSetImageToCtrl($img, "HERE") EndFunc and I call this func like that
      makeHelpImgGUI("Image",1190, 800,$SETTINGS_JPG)  
      so what is the problem in the parameter where is - "HERE" I need value of img but passed as string
      so $img = $SETTINGS_JPG and how make it "SETTINGS_JPG"
       
      I tried something like that but not work
      Func makeHelpImgGUI($title,$width,$height,$img) $name_str = String($img) $name_str = StringTrimLeft ($name_str, 1 ) $img = GUICtrlCreatePic("",20,40,$width,$height) _ResourceSetImageToCtrl($img, $name_str) EndFunc  
    • By xiantez
      This script used to work on an older version of AutoIT. Currently I am running AutoIT v3.3.14.5 and it's failing.
      Func PublicIP() ;Post public facing IP address Local $url = 'https://www.google.com/search?client=opera&q=what+is+my+ip&sourceid=opera&ie=UTF-8&oe=UTF-8' Local $getIPaddress = BinaryToString(InetRead($url)) Local $sStart = 'clamp:2">' Local $sEnd = '</div>' Local $ipaddress = _StringBetween($getIPaddress, $sStart, $sEnd For $i In $ipaddress MsgBox(0, 'External IP', "Your public IP address is " & $i) Next EndFunc ;==>PublicIP The console output shows:
      "C:\Users\user\Documents\AutoIT\Scripts\WSI Tools.au3" (197) : ==> Variable must be of type "Object".: For $i In $ipaddress For $i In $ipaddress^ ERROR ->14:12:16 AutoIt3.exe ended.rc:1 +>14:12:16 AutoIt3Wrapper Finished. >Exit code: 1 Time: 9.811
×
×
  • Create New...