Jump to content
Sign in to follow this  
Fran

Functions and me

Recommended Posts

Fran

Hope M23 is watching my post :blink:

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 by Fran

Share this post


Link to post
Share on other sites
ajit

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

Share this post


Link to post
Share on other sites
Fran

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.

F

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

Share this post


Link to post
Share on other sites
JohnOne

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.

Share this post


Link to post
Share on other sites
Melba23

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! :blink:

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._______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
Fran

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

Share this post


Link to post
Share on other sites
Fran

Oh my word!

I must be in "stupid-mode" today. :blink:

The RegRead script which works out the $appath was commented out.

Sorry for wasting your brilliant minds on my senseless post ;)

Share this post


Link to post
Share on other sites
Melba23

Fran,

The advice about error checking is still valid. :blink:

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._______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
Fran

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

Share this post


Link to post
Share on other sites
Melba23

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. :blink:

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!). :P

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
Fran

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 :blink:

Attached is the ini file.

F

AlbumMaker.txt

Share this post


Link to post
Share on other sites
Melba23

Fran,

This seems to work for me: ;)

#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. :blink:

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
Fran

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.

Share this post


Link to post
Share on other sites
Fran

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 by Fran

Share this post


Link to post
Share on other sites
JohnOne

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.

Share this post


Link to post
Share on other sites
Fran

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

Share this post


Link to post
Share on other sites
Melba23

JohnOne,

the " = " spaces dont matter in an inifile

Good call - something to stick away in the memory banks. Thanks. :blink:

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, declarations

300 lines creation of GUIs, traymenus, etc

100 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. :P

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
Fran

By the way, do look into Regions - a really useful way of hiding large chunks of the code within SciTE

Oh, what a difference it makes. It's so easy to find stuff now!

M23 for president!

F

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.