Jump to content

Create a variable with a variable in its name?


Recommended Posts

I have done a lot of searching prior to posting in hopes that I would be able to answer my own question... Sadly I was unable to locate an answer and I now turn to the masses for assistance.

What I would like to do is call a variable that has a variable -in- it's name. Is there any way to do this?

Example:

Dim $array[3] = ["2","Fred","Smith"]

for $i = 1 to $array[0]
     Global $user$array[$i] = "Placeholder for variable or array"
Next

consolewrite($userFred & " - - " & $userSmith & @CRLF)

I seem to recall is some other web language you were able to concatenate with a "." to append a variable to another variable to create a dynamic variable name. Is there any way to do this in AutoIt? I'm trying to avoid some nasty nested arrays in a rather large script that I have where I define each variable manually (very repetitive) and I was looking for a way to avoid super nested arrays...

I thank you for any assistance that may be rendered and your time in reviewing this post! :-]

-K

Edited by Keltset

-K

Link to comment
Share on other sites

I also tried to find that, I just use arrays instead of that... And in your case you have to use multidimensional arrays I think.

PowerSlide2UnlockiPhone look-a-like program you can use to lock your pc, you can't access your desktop again until the password is entered and the slider slided to the right. (really proud of it)-- After a time AutoIt and Windows, I switched to Mac. Don't expect me to answer anymore.
Link to comment
Share on other sites

I thought the same saywell with Assign() too, but as expected it didn't work.

Global $aArray[3] = [2, "Fred", "Smith"] ; Instead use Local or Global
For $i = 1 To $aArray[1]
     Assign("user" & $aArray[$i], $aArray[$i])
Next
ConsoleWrite(Eval("userFred") & " - - " & Eval("userSmith") & @CRLF)

UDF List:

 
_AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Thank you very much, I was playing with the Eval code and the code posted by guinness and it looks like this works as desired!

Global $aArray[3] = [2, "Fred", "Smith"]
Global $bArray[3] = [2, "fig1", "fig2"]
For $i = 1 To $aArray[0]
     Assign("user" & $aArray[$i], $aArray[$i])
 Next
 
ConsoleWrite("1| " & Eval("user" & $aArray[1]) & " - - " & Eval("userSmith") & @CRLF)
ConsoleWrite("2| " & $userFred & " - - " & $userSmith & @CRLF)
$userFred = $bArray
For $i = 1 To $aArray[0]
     Assign("user" & $aArray[$i], $bArray)
Next
 
ConsoleWrite("3| " & $userFred[1] & " - - " & $userFred[2] & @CRLF)
ConsoleWrite("4| " & $userSmith[1] & " - - " & $userSmith[2] & @CRLF)

This works very well when they are variables and not arrays. Thanks you, this alone will help me greatly. To finish piecing this puzzle together I have one last question. Is there a way to use Eval to call an element of an array? Something like:

ConsoleWrite(Eval("user" & $aArray[1])[1])

-K

Link to comment
Share on other sites

I believe Eval() can only be used with a String (but I could be wrong.) - I was wrong! Edited by guinness

UDF List:

 
_AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Is there a way to use Eval to call an element of an array?

assign it first?

Global $aArray[3] = [2, "Fred", "Smith"] ; Instead use Local or Global

For $i = 1 To ubound($aArray) - 1
     Assign("user" & $aArray[$i], $aArray[$i])
 Next

ConsoleWrite(Eval("userFred") & " - - " & Eval("userSmith") & @CRLF)


assign ("Lname" , $aArray[2] , 1)
ConsoleWrite(Eval("Lname") & @CRLF)

;same as

ConsoleWrite($aArray[2] & @CRLF)

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

Link to comment
Share on other sites

keltset,

I'm trying to avoid some nasty nested arrays in a rather large script that I have where I define each variable manually (very repetitive) and I was looking for a way to avoid super nested arrays...

What is a nested array?

What repetition, definition is done once? If you are cloning sections of code from script to script then just isolate the common code and #include<this_code.au3> from an include file.

Can you further define your requirements? I would encourage you to explore all alternatives before using any kind of self modifying code, for a variety of reasons.

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

Link to comment
Share on other sites

What is a nested array?

What repetition, definition is done once?...

What I was calling a nested array is an array inside an array inside an array etc... This is all contained in a single script so include is not very relevant to this situation.

My script generates about 40-50 excel spreadsheets from several datasources each morning. The main problem I was having is that I had been defining arrays for each user, and each type of report manually:

global $user1[25] = [24, "element one", "element two", etc..]
global $user2[25] = [24, "element one", "element two", etc..]
global $user3[25] = [24, "element one", "element two", etc..]
global $user4[25] = [24, "element one", "element two", etc..]

Then each element would contain another array or variable depending on the purpose. I was trying to avoid non similar elements in an array and easy addition of extra users, or the removal there of. With the use of a variable in the variable name it is easier to add a user (or remove) and not have to generate new arrays for each user for each type of report generated. Working with the following can become overly complex when you start compounding:

global $subArrayAFred[25] = [24, "element one", "element two", etc..]
global $subArrayASam[25] = [24, "element one", "element two", etc..]
global $subArrayAJasper[25] = [24, "element one", "element two", etc..]

global $subArrayABFred[25] = [24, "element one", "element two", etc..]
global $subArrayABSam[25] = [24, "element one", "element two", etc..]
global $subArrayABJasper[25] = [24, "element one", "element two", etc..]

global $subArrayBFred[25] = [24, $subArrayAFred, subArrayABFred, etc..]
global $subArrayBSam[25] = [24, $subArrayASam", $subArrayABSam, etc..]
global $subArrayBJasper[25] = [24, $subArrayAJasper, $subArrayABJasper, etc..]

global $subArrayCFred[25] = [24, "element one", $subArrayBFred, etc..]
global $subArrayCSam[25] = [24, "element one", $subArrayBSam", etc..]
global $subArrayCJasper[25] = [24, "element one", $subArrayCJasper", etc..]

Traversing these becomes to complex to follow without a map. By being able to do $reportOneFred, $reportTwoFred, etc.. it becomes easier to write new reports or modify existing reports and user management becomes less difficult. With the variable included in variable name method I do not need to constantly traverse down an arrays tree to locate the necessary information to manipulate or use.

Edited by Keltset

-K

Link to comment
Share on other sites

keltset,

Let me offer an opinion. This sounds like a job that has grown beyond the origional implementation. I know that it's hard to bite off a re-think of something that is working, albeit, clumsily. Have you considered a database solution?

There are some really sharp DBA types here that can chime in on this..

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

Link to comment
Share on other sites

I appreciate the input, actually I -wanted- to do this as a database because it resolves all of my issues. The problem I have is that I have very limited access on the computer that I'm running this script on and have had to come up with a work around. AutoIt doesn't require any special permissions to run. I definitely know that this project has reached a point where database implementation would be a better approach, but I suppose I have to work with the tools I have available. :)

-K

-K

Link to comment
Share on other sites

Using an sqlite database does not require any special permissions. All you need is the sqlite.dll and the database is stored as a flat file.

Thank you, I will have to look into the use of sqlite as a better implementation of this. I see that there is a sqlite UDF. I suppose it's time for me to dig in and learn how to work with this. This sounds like the better route to go, over my clumsy script that get's by. I appreciate your pointing me towards this!

Thanks,

-K

-K

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...