Mbee Posted July 4, 2014 Share Posted July 4, 2014 I've used _FileListToArray() many times, but I've never encountered the curious situation I'm seeing today. Consider the following code: #include <Array.au3> #include <Constants.au3> #include <File.au3> ; Global $G_FileList[1] $G_FileList = _FileListToArray("C:\", "*.txt", $FLTA_FILES) If @error = 4 Then ; 4 = No such files If IsArray($G_FileList) Then MsgBox($MB_OK, "File Array Test", "FileList IS an array, with # " & $G_FileList[0] & " elements") Else MsgBox($MB_OK, "File Array Test", "FileList is NOT an array!") EndIf EndIf Exit When there are no .txt files in the root directory, I see the "NOT an array" message, and thus the _FileListToArray() function seems to actually UN-declare $G_FileList as an array! Instead, I was expecting it to return an array with only 1 element, and $G_FileList[0] = 0 (although I will grant that the help file does not say that). So what's the best solution? Something like the following? #include <Array.au3> #include <Constants.au3> #include <File.au3> ; Global $G_FileList[1] $G_FileList = _FileListToArray("C:\", "*.txt", $FLTA_FILES) If @error = 4 Then ; 4 = No such files If Not IsArray($G_FileList) Then Global $G_FileList[1] $G_FileList[0] = 0 EndIf EndIf Exit Link to comment Share on other sites More sharing options...
jguinch Posted July 4, 2014 Share Posted July 4, 2014 Maybe something like this : #include <Constants.au3> #include <File.au3> Local $aEmpty[1] = [0] Local $aFileList = _FileListToArray("C:\", "*.exe", 0) If @error = 4 Then $aFileList = $aEmpty If IsArray($aFileList) Then MsgBox($MB_OK, "File Array Test", "FileList IS an array, with # " & $aFileList[0] & " elements") Else MsgBox($MB_OK, "File Array Test", "FileList is NOT an array!") EndIf Mbee 1 Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
Mbee Posted July 4, 2014 Author Share Posted July 4, 2014 Thanks, jguinch; that works too, and is neater. I was just surprised by _FileListToArray()'s behavior... Maybe something like this : #include <Constants.au3> #include <File.au3> Local $aEmpty[1] = [0] Local $aFileList = _FileListToArray("C:\", "*.exe", 0) If @error = 4 Then $aFileList = $aEmpty If IsArray($aFileList) Then MsgBox($MB_OK, "File Array Test", "FileList IS an array, with # " & $aFileList[0] & " elements") Else MsgBox($MB_OK, "File Array Test", "FileList is NOT an array!") EndIf Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted July 4, 2014 Moderators Share Posted July 4, 2014 Mbee,This is a similar point to the one made here about _FileReadToArray - and the answer is unsurprisingly the same. It was a design decision to return a non-array variable if an error occurred from most of the functions which would return an array if successful. If an error is flagged, the user should have errorchecking code to determine what error occurred and how then to proceed - this code can then reset the return variable to whatever is required for the code to continue to run as desired. It is impossible to return a single return value which would suit every case - setting the error macro is the only reasonable thing to do as checking that single item allows each user to proceed as they wish. And a suggestion of such errorchecking:; Run function Local $aFileList = _FileListToArray("C:\", "*.exe", 0) ; If no files found then reset array If @error = 4 Then Local $aFileList[1] = [0]Now you can be sure that the variable is an array declared as you want it. M23 Mbee 1 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...
Mbee Posted July 4, 2014 Author Share Posted July 4, 2014 Thanks, Melba23! You know, I really am careful to perform due diligence and search the forums before posting a question, but alas, my GoogleFu failed me this time. I didn't think of performing a blanket search for _FileListToArray in the thread titles, and my pitiful memory is such that I can't recall everything in the change logs and script-breaking docs However, as I indicated in my OP, I did indeed recognize that the help file entry for _FileListToArray() never promised any defined return value if an error occurred (other than @error), so the documentation certainly was not wrong. However, would it be hopelessly rude and naive of me to suggest perhaps adding a note in the Remarks section of the help file to explicitly clarify this behavior? (Just an idea...) Anyway, the thread you pointed me to explains why I haven't run into this behavior prior to v3.3.12.0. So now, it's off to check my old scripts for sloppy error handling! Thanks again! Mbee, This is a similar point to the one made here about _FileReadToArray - and the answer is unsurprisingly the same. It was a design decision to return a non-array variable if an error occurred from most of the functions which would return an array if successful. If an error is flagged, the user should have errorchecking code to determine what error occurred and how then to proceed - this code can then reset the return variable to whatever is required for the code to continue to run as desired. It is impossible to return a single return value which would suit every case - setting the error macro is the only reasonable thing to do as checking that single item allows each user to proceed as they wish. And a suggestion of such errorchecking: ; Run function Local $aFileList = _FileListToArray("C:\", "*.exe", 0) ; If no files found then reset array If @error = 4 Then Local $aFileList[1] = [0] Now you can be sure that the variable is an array declared as you want it. M23 Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted July 4, 2014 Moderators Share Posted July 4, 2014 Mbee, my GoogleFu failed me this timeNo problem - I cannot remember all the changes either! the help file entry for _FileListToArray() never promised any defined return value if an error occurred[...]perhaps adding a note in the Remarks section of the help file to explicitly clarify this behaviourI think not - as then we would have to try and make sure that future changes to the function respected the same behaviour. When we rewrite the UDFs (usually to add more functionality) it is difficult enough trying to prevent too many script-breaking changes - defining more closely what happens in error cases would merely be making life much harder for ourselves - or whoever does it in the future. check my old scripts for sloppy error handling!I would encourage everyone to do this. And perhaps to regard events like this as a opportunity to tighten up the code rather than a problem - I freely admit I had to change some of my old code where I had been less than rigorous. 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...
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