leuce Posted January 8, 2014 Share Posted January 8, 2014 (edited) Hello everyoneI have this string: $foo = 'one="apple" two="berry" three="cherry"'And I want to convert it to this set of variables: $one = "apple" $two = "berry" $three = "cherry"Is there is a simple (potentially one-line) way of accomplishing it?ThanksSamuel Edited January 8, 2014 by leuce Link to comment Share on other sites More sharing options...
leuce Posted January 8, 2014 Author Share Posted January 8, 2014 (edited) Hello everyone[This question is for solving the same problem as mentioned in this forum post.]Is it possible to convert a string that is essentially a chunk of AutoIt code into actual AutoIt code that is executed by the script? For example, if I have this: $foo = '$one = "apple"'can I tell the script to treat $foo as if it is the following AutoIt instruction: $one = "apple"ThanksSamuel Edited January 8, 2014 by leuce Link to comment Share on other sites More sharing options...
iamtheky Posted January 8, 2014 Share Posted January 8, 2014 (edited) ;~ Global $one , $two , $three $foo = 'one="apple" two="berry" three="cherry"' $Afoo = stringsplit(StringStripWS($foo, 8) , '" ') for $i = 1 to $Afoo[0] - 1 step 2 Assign(stringtrimright($Afoo[$i], 1), $Afoo[$i+1]) next ;~ msgbox(0, '' , $one) ;~ msgbox(0, '' , $two) ;~ msgbox(0, '' , $three) Edited January 8, 2014 by boththose ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
Gianni Posted January 8, 2014 Share Posted January 8, 2014 take a look to these functions: Assign, Execute, Eval Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
leuce Posted January 8, 2014 Author Share Posted January 8, 2014 ...Thanks, Boththose, that goes a long way to solve my problem. I notice, however, that it must be known beforehand that there will have to be variables named $one, $two and $three. In other words, if I as the script writer doesn't know beforehand that the string will contain "one", "two" and "three", then I would not be able to do this, right? Link to comment Share on other sites More sharing options...
iamtheky Posted January 8, 2014 Share Posted January 8, 2014 nope, that was only for the msgbox, i realized that and commented them out in the above. Though i see not quickly enough. It is not necessary to declare, it just fails au3check. ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
leuce Posted January 8, 2014 Author Share Posted January 8, 2014 (edited) Take a look to these functions:Assign, Execute, EvalYes, I was looking at those, thanks. However, "Execute" only executes an expression, not code, as in this non-working example: $foo = '$bar="hello"' Execute ($foo) MsgBox (0, "", $bar, 0) ; Expected: get message box that says 'hello'Do you know of a different function that I could use instead of "Execute" that would work exactly like this?==Added:I tried #include, but it would seem that #include only works if the file already exists at the time that the script is run. This, for example, does not work:FileWrite (FileOpen ("hello.au3", 130), '$bar="hello"') #include "hello.au3" MsgBox (0, "", $bar, 0)ThanksSamuel Edited January 8, 2014 by leuce Link to comment Share on other sites More sharing options...
guinness Posted January 8, 2014 Share Posted January 8, 2014 Why the double post? 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 More sharing options...
leuce Posted January 8, 2014 Author Share Posted January 8, 2014 Why the double post?It's not a double post. It's two potentially different solutions to a single problem. Yes, I have one problem, but I can think of two ways to solve it, and I'm therefore asking two separate questions about the two separate potential solutions. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted January 8, 2014 Moderators Share Posted January 8, 2014 leuce, Yes, I have one problemThen stick to the one thread - threads merged. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
iamtheky Posted January 8, 2014 Share Posted January 8, 2014 Nope. Its the same question twice. That discussion should include all potential answers. Search would be ridiculous if I had to do so by potential solution and not by the question. ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
leuce Posted January 8, 2014 Author Share Posted January 8, 2014 (edited) Yes, I have one problem...Then stick to the one thread - threads merged. No problem, thanks for the merge. Nope. Its the same question twice. That discussion should include all potential answers. Search would be ridiculous if I had to do so by potential solution and not by the question.I see your point.==So, in this thread, my the one question (i.e. how to convert a string into a variable) has been answered by boththose -- thanks! That solution will simplify matters considerably.I'm still wondering about the second question, though... is it possible to execute the content of a variable? I suppose I could split what I'm trying to do into two scripts -- one to create an #include file and the second to use the #include file.Samuel Edited January 8, 2014 by leuce Link to comment Share on other sites More sharing options...
leuce Posted January 8, 2014 Author Share Posted January 8, 2014 $foo = 'one="apple" two="berry" three="cherry"' $Afoo = stringsplit(StringStripWS($foo, 8) , '" ') for $i = 1 to $Afoo[0] - 1 step 2 Assign(stringtrimright($Afoo[$i], 1), $Afoo[$i+1]) nextThanks again. The string I had contained spaces that had to be preserved, so I could not use your exact solution in the end, but it really helped me to get to what I ended up with:$properties1 = 'ConcID="1" value="xyz" ProdName="xyz" LingInf="Noun | Full Form" Desc="xyz." Stat="Approved" ' $properties2 = StringSplit($properties1, '" ', 1) For $k = 1 to $properties2[0] - 1 $properties3 = StringSplit($properties2[$k], '="', 1) If $properties3[0] = 2 Then Assign($properties3[1], $properties3[2]) EndIf Next Link to comment Share on other sites More sharing options...
l3ill Posted January 8, 2014 Share Posted January 8, 2014 is it possible to execute the content of a variable? Yes, but it will be a different command depending on what the vaiable contains, for instance: Path - FileOpen EXE - ShellExecute PDF - Run Etc... My Contributions... SnippetBrowser NewSciTE PathFinder Text File Manipulation FTP Connection Tester / INI File - Read, Write, Save & Load Example Link to comment Share on other sites More sharing options...
leuce Posted January 8, 2014 Author Share Posted January 8, 2014 Yes, but it will be a different command depending on what the variable contains, for instance:Path - FileOpenEXE - ShellExecutePDF - RunEtc...I'm intriged by your answer. Could you please give me an example? In my case, the variable contains a string, and the string is: $foo = '$bar="hello"'. Link to comment Share on other sites More sharing options...
jdelaney Posted January 8, 2014 Share Posted January 8, 2014 (edited) $properties1 = 'ConcID="1" value="xyz" ProdName="xyz" LingInf="Noun | Full Form" Desc="xyz." Stat="Approved" ' $a = StringRegExp($properties1,"(?U)\s?(\w+)=""([^""]+)""",4) For $i = 0 To UBound($a) - 1 $aTemp = $a[$i] Assign($aTemp[1],$aTemp[2],2) Next ConsoleWrite("ConcID=[" & Eval("ConcID") & "]" & @CRLF) ConsoleWrite("value=[" & Eval("value") & "]" & @CRLF) ConsoleWrite("ProdName=[" & Eval("ProdName") & "]" & @CRLF) ConsoleWrite("LingInf=[" & Eval("LingInf") & "]" & @CRLF) ConsoleWrite("Desc=[" & Eval("Desc") & "]" & @CRLF) ConsoleWrite("Stat=[" & Eval("Stat") & "]" & @CRLF) Output: ConcID=[1] value=[xyz] ProdName=[xyz] LingInf=[Noun | Full Form] Desc=[xyz.] Stat=[Approved] Edited January 8, 2014 by jdelaney IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window. Link to comment Share on other sites More sharing options...
l3ill Posted January 8, 2014 Share Posted January 8, 2014 There are plenty of goo-roos here that can answer this better than I but you have the suggestion from PincoPanco in post # 4. These are some of the functions that can execute a string. The execute command itself does not look like it is designed for this particular jumble as it executes the expression which as I understand it is some sort of a value (number math etc). My post was not necessarily confining the execution of the variable to the actual command known as "execute". Bill My Contributions... SnippetBrowser NewSciTE PathFinder Text File Manipulation FTP Connection Tester / INI File - Read, Write, Save & Load Example Link to comment Share on other sites More sharing options...
Celtic88 Posted January 8, 2014 Share Posted January 8, 2014 I'm interested in can be used to protect script Link to comment Share on other sites More sharing options...
Gianni Posted January 8, 2014 Share Posted January 8, 2014 ..... I suppose I could split what I'm trying to do into two scripts -- one to create an #include file and the second to use the #include file. Samuel Hi leuce using your script from post >#13 you could create your "include" containing the declarations of your variables in this way: $properties1 = 'ConcID="1" value="xyz" ProdName="xyz" LingInf="Noun | Full Form" Desc="xyz." Stat="Approved" ' $properties2 = StringSplit($properties1, '" ', 1) $path = FileOpen("hello.au3", 130) For $k = 1 To $properties2[0] - 1 $properties3 = StringSplit($properties2[$k], '="', 1) If $properties3[0] = 2 Then FileWriteLine($path, "$" & $properties3[1] & ' = "' & $properties3[2] & '"') EndIf Next FileClose($path) and then include the generated hello.au3 in your second script. Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
BrewManNH Posted January 8, 2014 Share Posted January 8, 2014 I'm interested in can be used to protect script No, you can't protect your script. You can make it harder to decode it, but your script will never be 100% safe. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now