Fran Posted July 8, 2010 Posted July 8, 2010 (edited) Hope M23 is watching my post I have tried splitting up my script in a bunch of functions, so everything is not so messed up where I'm working. And then as I need the stuff to happen, I just call the relevant function. (is that the right way of doing this?) Got a problem with one of my little functions though... Func _RS_InstallHelpBoxes() ;Install help boxes $file = FileOpen($appath & "\MPR500 Pro 5\AlbumMaker.ini", 0) $line = FileReadLine($file,10) $submitline = StringSplit($line, ' = ', 1) $submitpath = $submitline[2] FileInstall("Help submit my files.exe", $submitpath & "\") EndFunc ;==> _RS_InstallHelpBoxes ($appath - global variable - is declared in a function called before this one) If I call this function, I get an ERROR: Y:\Perm\rapidstudio\patches and updates\ContentUpdate.au3 (264) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.: Dim $submitpath = $submitline[2] Dim $submitpath = ^ ERROR And for the life of me, I can't figure out why. So far the other functions are working perfectly. F Edited July 8, 2010 by Fran
ajit Posted July 8, 2010 Posted July 8, 2010 Just a guess May be your 10th line in the ini file is "=" and not " = ". I removed the spaces surrounding "=" and it worked well for me. Thus $submitline = StringSplit($line, '=', 1) and NOT $submitline = StringSplit($line, ' = ', 1) Ajit
Fran Posted July 8, 2010 Author Posted July 8, 2010 Thanx Ajit...but the ini file has spaces before and after the "="and if I run this little script just like this within my main function, it works fine.The Error only occurs when I make this little piece of script of function and then call it.FJust a guessMay be your 10th line in the ini file is "=" and not " = ". I removed the spaces surrounding "=" and it worked well for me.Thus $submitline = StringSplit($line, '=', 1) and NOT $submitline = StringSplit($line, ' = ', 1) Ajit
JohnOne Posted July 8, 2010 Posted July 8, 2010 Why dont you use iniread to get the value? $submitline = IniRead("Path to ini","SectionName","KeyName","") Also you mention you are declaring a global variable in a function, thats not going to work out for you, if you meant it literally. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans.
Moderators Melba23 Posted July 8, 2010 Moderators Posted July 8, 2010 Fran,Your error arises because you do not get an array returned from your StringSplit call - either you do not get a valid line from your file or you have the delimiter set wrongly as suggested earlier. You must put in error checking if you expect arrays to be returned - IsArray is a good place to start! But you really should look at the Ini functions in the Help file. They make storing and recovering values from ini files very easy - no need to open, read, split and close your own files, just a simple IniRead call. That would solve this problem by removing it completely. 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
Fran Posted July 8, 2010 Author Posted July 8, 2010 Thanx... I'll have a look. I've tried iniRead, but because my ini does not have a section name, it doesn't want to work. Still... if I don't call the function and just put the 3 lines of script in my Main section, it works perfectly. maybe the problem is with the $appath variable as JohnOne suggested. F
Fran Posted July 8, 2010 Author Posted July 8, 2010 Oh my word! I must be in "stupid-mode" today. The RegRead script which works out the $appath was commented out. Sorry for wasting your brilliant minds on my senseless post
Moderators Melba23 Posted July 8, 2010 Moderators Posted July 8, 2010 Fran, The advice about error checking is still valid. Why does your ini not have sections titles? Did you create it for your script or is it a preexisting file? 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
Fran Posted July 8, 2010 Author Posted July 8, 2010 Why does your ini not have sections titles?It's a preexisting file... as to "WHY?" - I don't ask that question anymore when it comes to the developers of that certain piece of software... lol - those guys are something else.Fran
JohnOne Posted July 8, 2010 Posted July 8, 2010 If it were me, I'd just write a section name to it, or create a standard ini from the file. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans.
Moderators Melba23 Posted July 8, 2010 Moderators Posted July 8, 2010 Fran, JohnOne has a point - all you need to do is add a single line of [] at the top of the file and you have a valid ini file. Ah, I have just seen this: but the ini file has spaces before and after the "="Well, a simple StringReplace can sort that. Could you provide a sample of the original non-standard ini file (suitably sanitised if necessary ) and we will see what we can do. A small function should be able to convert the files back and forth without too many problems (he says bravely!). 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
Fran Posted July 8, 2010 Author Posted July 8, 2010 A small function should be able to convert the files back and forth without too many problems (he says bravely!). I am not worried at all Attached is the ini file.FAlbumMaker.txt
Moderators Melba23 Posted July 8, 2010 Moderators Posted July 8, 2010 Fran, This seems to work for me: expandcollapse popup#include <File.au3> $sOrgFile = @ScriptDir & "\AlbumMaker.txt" $sIniFile = @ScriptDir & "\AlbumMaker.ini" ; Create a normal ini file from the txt file _Create_Ini() ; Read from the ini file $sDemoURL = IniRead($sIniFile, "", "DEMO_URL", "WTF") ConsoleWrite($sDemoURL & @CRLF) ; Write to the ini file IniWrite($sIniFile, "", "DEMO_URL", "This is a new URL") ; Reread from the ini file $sDemoURL = IniRead($sIniFile, "", "DEMO_URL", "WTF") ConsoleWrite($sDemoURL & @CRLF) ; Convert the ini file back into a txt file _Save_Ini() Func _Create_Ini() ; Read in contents of txt file $sFileContents = FileRead($sOrgFile) ; Add Section line and remove the spaces around the = $sIniContents = "[]" & @CRLF & StringReplace($sFileContents, " = ", "=") ; Overwrite any existing ini file $hFile = FileOpen($sIniFile, 2) FileWrite($hFile, $sIniContents) FileClose($hFile) EndFunc Func _Save_Ini() Local $aFileContents ; Get the ini contents into an array _FileReadToArray($sIniFile, $aFileContents) ; Put spaces around the FIRST = sign only For $i = 2 To $aFileContents[0] $aFileContents[$i] = StringReplace($aFileContents[$i], "=", " = ", 1) Next ; Write the file - except the first 2 elements (element count & false section []) _FileWriteFromArray($sOrgFile, $aFileContents, 2) EndFunc Give it a try and let me know if it works for you too. 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
Fran Posted July 8, 2010 Author Posted July 8, 2010 mmmm........ Now the application crashes! lol (by the way... the original file is an ini file, but I couldn't upload it, so I changed the extension to txt) I know when I was making changes to the ini files before using a batch script, I had to convert the file first. from UTF-16LE to UTF-8 otherwise the ini corrupted.
Fran Posted July 8, 2010 Author Posted July 8, 2010 (edited) ok... this is what I've done now and it seems to work 100% if FileReadLine($appath & "\MPR500 Pro 5\AlbumMaker.ini",1) = "[]" Then _FileWriteToLine($appath & "\MPR500 Pro 5\AlbumMaker.ini", 1, "[]", 1) Else _FileWriteToLine($appath & "\MPR500 Pro 5\AlbumMaker.ini", 1, "[]", 0) EndIf $submitpath = IniRead($appath & "\MPR500 Pro 5\AlbumMaker.ini", "", "BASE_SUBMIT_FOLDER", "C:\RapidStudio_Submitted_Orders") comments? Edited July 8, 2010 by Fran
JohnOne Posted July 8, 2010 Posted July 8, 2010 Heres my take it dosent have to be named ini to be treated like one and the " = " spaces dont matter in an inifile' #include <File.au3> $file = "AlbumMaker.txt" _FileWriteToLine($file,1,"[MYSECTION]") $ret = IniRead($file,"MYSECTION","FTP_PAY_URL","Fail") ConsoleWrite($ret & @LF) AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans.
Fran Posted July 8, 2010 Author Posted July 8, 2010 Heres my take it dosent have to be named ini to be treated like one and the " = " spaces dont matter in an inifile' #include <File.au3> $file = "AlbumMaker.txt" _FileWriteToLine($file,1,"[MYSECTION]") $ret = IniRead($file,"MYSECTION","FTP_PAY_URL","Fail") ConsoleWrite($ret & @LF) Thanx! Finally got it. I'm making a copy of the ini file to the tempdir and then I writetoline, instead of changing the original ini file - cause then the program crashes. Thank you so much for all your help! (Everybody) on another note: I still want to know if it's better to write a bunch of functions and then calling them in the main function, or just to do everything within the main script (without a bunch of functions at the bottom) Fran
Moderators Melba23 Posted July 8, 2010 Moderators Posted July 8, 2010 JohnOne,the " = " spaces dont matter in an inifileGood call - something to stick away in the memory banks. Thanks. Fran,I still want to know if it's better to write a bunch of functions and then calling them in the main function, or just to do everything within the main script (without a bunch of functions at the bottom)My view - the former. If you break down the code into functions then any changes can be made simply to the relevant functions. It makes the code easier to read - and therefore to maintain.I have just looked at one of my bigger scripts and this is the approximate breakdown:100 lines directives, includes, declarations300 lines creation of GUIs, traymenus, etc100 lines of GUIGetMsg loop (lots of controls, but each Case has one line pointing to a function)3900 lines of functions various, all broken down into regions.Now all these figures include comment lines and blank lines for spacing, but they give you the idea of the relative sizes. And during the last change (when I added an item to a dialog) I had only to open one function to amend the code.By the way, do look into Regions - a really useful way of hiding large chunks of the code within SciTE - that script above fits in one page when fully folded. 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
Fran Posted July 9, 2010 Author Posted July 9, 2010 By the way, do look into Regions - a really useful way of hiding large chunks of the code within SciTEOh, what a difference it makes. It's so easy to find stuff now!M23 for president!F
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