IAMK Posted November 29, 2018 Posted November 29, 2018 AutoIT_Config.txt: $market="M1" ;Market options: M1, M2, M3. Script: #include <File.au3> Local $configFile = "AutoIT_Config.txt" ;The file to read for setting up the below variables. Local $fin = FileOpen($configFile, 0) ;Open the file. Local $market = 0 ;Which market to run the script for. For $i = 1 To _FileCountLines($configFile) ;Read the configuration textfile into an array. $temp = StringSplit(FileReadLine($fin, $i), "=", 0) _ArrayDisplay($temp) If(StringStripWS($temp[1], $STR_STRIPALL) = "$market") Then ;Execute($market = $temp[2]) ;Attempt 1 ;$market = Execute($temp[2]) ;Attempt 2 EndIf Next MsgBox(0, "DEBUG", $market) FileClose($fin) ;Close the file. What I am trying to do is have a .exe and a .txt file. This way, people will not need to edit the script and recompile, etc. Inside the .txt file, you declare the variables. The issue is that the variables have comments to explain the options available. I don't want to use ; as a delim because the variable I wish to set to may also be a string which has a ; in it. E.g. $market="Market1; Market2" ;This is a comment. Basically what I am trying to do is read a line of AutoIT code into the script from a .txt file. Thank you in advance.
FrancescoDiMuro Posted November 29, 2018 Posted November 29, 2018 @IAMK Why don't you use an .ini file? Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette
KickStarter15 Posted November 29, 2018 Posted November 29, 2018 Yup Francesco is right, why not use and .ini file instead of .txt file. You can change your comment from this ";" to this "<!-- Market options: M1, M2, M3. -->" instead. Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.
IAMK Posted November 29, 2018 Author Posted November 29, 2018 I don't think this solves my issue. I want anything to be writable after the first "=" (as long as it's in 1 line in the file. Correct me if I misunderstood.
FrancescoDiMuro Posted November 29, 2018 Posted November 29, 2018 (edited) @IAMK Could be this a possible solution? Comments are above the variables, so you can store as much as values you want delimited with ";" Content of the INI File: [VARIABLES] ;This is a comment for Variable1 Variable1=M1;M2;M3 ;This is a comment for Variable2 Variable2=This is the Second Variable Script: #include <Array.au3> Global $strINIFilePath, _ $strSectionName = "VARIABLES", _ $arrSectionValues $strINIFilePath = @ScriptDir & "\Variables.ini" $arrSectionValues = IniReadSection($strINIFilePath, $strSectionName) If @error Then ConsoleWrite("Error while reading the section '" & $strSectionName & "' from the file '" & $strINIFilePath & "'. Error: " & @error & @CRLF) Else _ArrayDisplay($arrSectionValues) EndIf Edited November 29, 2018 by FrancescoDiMuro Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette
Subz Posted November 29, 2018 Posted November 29, 2018 So how do you propose the script is going to recognize a comment vs a value? $market="Market1; Market2" ;This is a comment vs $market="Market1; This is a comment Like the other posters I'd recommend an Ini format, something like: [Variables] ;~ Market Options: M1, M2, M3 Market = M2 The code: ;~ If the Market Key is missing default to M1 Local $sMarket = IniRead("Variables.ini", "Variables", "Market", "M1") ConsoleWrite($sMarket & @CRLF)
IAMK Posted November 29, 2018 Author Posted November 29, 2018 Okay, thanks. I will implement it that way then.
mikell Posted November 29, 2018 Posted November 29, 2018 A variation - still using ini [Variables] market="Market1; Market2" comment_market= "This is a comment" #include <Array.au3> $ini = @ScriptDir & "\ini.ini" $vars = IniReadSection($ini, "variables") _ArrayDisplay($vars) Assign("market", $vars[1][1]) Assign("comment_market", $vars[2][1]) Msgbox(0,"", "$market = " & $market & " ;" & $comment_market )
iamtheky Posted November 29, 2018 Posted November 29, 2018 (edited) The INI would be the default response for KV store, but I would like to see a few more actual examples beyond these two. Quote $market="M1" ;Market options: M1, M2, M3 $market="Market1; Market2" ;This is a comment. Are your keys going to be stored in the source text with a dollar sign already on them? Are your values going to already be in double quotes? If both of those are true i think this becomes a stringop game to speed things up. edit: could be regex'd by the person above me, but heres some dirty stringops ;~ $str = '$market="M1" ;Market options: M1, M2, M3' $str = '$market="Market1; Market2" ;This is a comment.' assign(stringtrimleft(stringsplit(stringleft($str , stringinstr($str , '"' , 0 , -1)) , "=" , 2)[0] ,1) , stringsplit(stringleft($str , stringinstr($str , '"' , 0 , -1)) , "=" , 2)[1]) msgbox(0, '' , eval("market")) Edited November 29, 2018 by iamtheky ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
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