Sign in to follow this  
Followers 0
leuce

How to convert a string into AutoIt code

20 posts in this topic

#1 ·  Posted (edited)

Hello everyone

I 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?

Thanks

Samuel

Edited by leuce

Share this post


Link to post
Share on other sites



#2 ·  Posted (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"
Thanks

Samuel

Edited by leuce

Share this post


Link to post
Share on other sites

#3 ·  Posted (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 by boththose

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

Share this post


Link to post
Share on other sites

take a look to these functions:
Assign, Execute, Eval


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

...

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?

Share this post


Link to post
Share on other sites

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.


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

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Take a look to these functions:

Assign, Execute, Eval

Yes, 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)

Thanks

Samuel

Edited by leuce

Share this post


Link to post
Share on other sites

Why the double post?


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_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 parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

leuce,

 

Yes, I have one problem

Then 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._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

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.


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

Share this post


Link to post
Share on other sites

#12 ·  Posted (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 by leuce

Share this post


Link to post
Share on other sites

$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

Thanks 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

Share this post


Link to post
Share on other sites

Yes, but it will be a different command depending on what the variable contains, for instance:

Path - FileOpen

EXE - ShellExecute

PDF - Run

Etc...

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"'.

Share this post


Link to post
Share on other sites

#16 ·  Posted (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 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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

I'm interested in can be used to protect script  :huh2: 

Share this post


Link to post
Share on other sites

.....

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.


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

I'm interested in can be used to protect script  :huh2: 

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 Gude
How 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

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