cpt2400 Posted October 14, 2009 Posted October 14, 2009 I have just started a little project with AutoIt. I have never used it before and want to make sure I am not headed down the wrong path. The script needs to compare two files. File 1 will be the standard, and File 2 will be compared to it. Any discrepencies to File 1 will be written to an error log. I have tried to include some error checking as well. A second point is the matter of arrays. The documentation says this ... "An Array is a variable containing series of data elements of the same type and size. Each element in this variable can be accessed by an index number. Before you can start using Arrays in your script, you must define their bounds using the 'Dim' keyword. " However, as you see in my code, while the Arrays are declared, they are not dimensioned. And each element is holding a string of varying length. Am I building something that is going to crash when I least expect it? Any comments will be appreciated. CODE FOLLOWS ; Use this as the basis for the file comparision script #include <file.au3> $Er_File = FileOpen("C:/exp/File_Error_Log.txt", 1) Dim $aFile Dim $bFile ; Large arrays are no problem. I put 8904 lines in, in no time. ; Apparently, arrays do NOT have to be dimensioned before they are used. ; Write another script to see if they can be dynamically resized. ; Doesn't work, 1st array is 'deleted' then refilled ; I am using text files from Project Gutenberg to simulate If Not _FileReadToArray("C:/exp/txt_01/DGrey.txt",$aFile) Then FileWriteLine( $Er_File, "C:/exp/txt_01/DGrey.txt could not be loaded into the array. Script will exit.") Exit EndIf If Not _FileReadToArray("C:/exp/txt_04/DGrey_bad.txt",$bFile) Then FileWriteLine( $Er_File, "C:/exp/txt_04/DGrey_bad.txt could not be loaded into the array. Script will exit.") Exit EndIf FileWriteLine( $Er_File, "C:/exp/txt_01/DGrey.txt is the Standard.") FileWriteLine( $Er_File, "C:/exp/txt_04/DGrey_bad.txt is being compared to the Standard.") FileWriteLine( $Er_File, " . . . . . . . . . . . . . . . . . . . . . . . . ") FileWriteLine( $Er_File, " . . . . . . . . . . . . ") If $aFile[0] > $bFile[0] Then FileWriteLine( $Er_File, "C:/exp/txt_04/DGrey_bad.txt has less lines than the Test Standard and will not be checked.") Exit EndIf If $aFile[0] < $bFile[0] Then FileWriteLine( $Er_File, "C:/exp/txt_04/DGrey_bad.txt has more lines than the Test Standard and will not be checked.") Exit EndIf For $x = 1 to $aFile[0] If $aFile[$x] <> $bFile[$x] Then FileWriteLine( $Er_File, "Line "& $x & " is different from the Standard.") FileWriteLine( $Er_File, "The Standard is : "& $aFile[$x] ) FileWriteLine( $Er_File, "The file being checked is : "& $bFile[$x] ) FileWriteLine( $Er_File, " . . . . . . . . . . . . ") EndIf Next FileClose( $Er_File ) MsgBox( 48, "Success", "How many records are there in 1st array : " & $aFile[0]) MsgBox( 48, "Success", "How many records are there in 2nd array : " & $bFile[0])
martin Posted October 14, 2009 Posted October 14, 2009 ...However, as you see in my code, while the Arrays are declared, they are not dimensioned.And each element is holding a string of varying length.Am I building something that is going to crash when I least expect it? Any comments will be appreciated.You must declare the variable with the number of elements or it is not an array. You can change the number of elements with ReDim and check the number of elements with UBound.Your code might work because _FileReadToArray will simply convert the variable you pass ByRef into an array, so in this case you do not need to declare it as an array and a simple Dim, Local or Global declaration would be ok.However, it might be wise to check that the variable is an array before you use it as one because _FileReadToioArray might fail. Something likeIf IsArray($aFile) then Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
cpt2400 Posted October 14, 2009 Author Posted October 14, 2009 You must declare the variable with the number of elements or it is not an array. You can change the number of elements with ReDim and check the number of elements with UBound.Your code might work because _FileReadToArray will simply convert the variable you pass ByRef into an array, so in this case you do not need to declare it as an array and a simple Dim, Local or Global declaration would be ok.However, it might be wise to check that the variable is an array before you use it as one because _FileReadToioArray might fail. Something likeIf IsArray($aFile) thenI did some more experimenting as you suggested. $aFile is NOT an array after it is declared, but it IS an array after calling _FileReadToArray.The code I posted works fine. It takes a text file that has 8904 lines and puts each line into an array element. Then I compare the arrays and single out the lines that are different.I looked at the function code for _FileReadToArray, and could not see how the array was loaded. As far as I can tell, there is no loop in the code.And I am very curious as to why the arrays I declare are static, but the array inside _FileReadToArray is (or appears to be) dynamic?Can anyone help?
Moderators Melba23 Posted October 14, 2009 Moderators Posted October 14, 2009 cpt2400,$aFile is NOT an array after it is declared, but it IS an array after calling _FileReadToArrayThis is because you have not declared $aFile with any dimensions. As martin pointed out, you must declare the size when you create an array. Just declaring the name merely declares a variable. In fact you can use this to destroy arrays which are no longer needed - from the Help file:To erase an array (maybe because it is a large global array and you want to free the memory), simply assign a single value to it:$array = 0. This will free the array and convert it back to the single value of 0.I am very curious as to why the arrays I declare are static, but the array inside _FileReadToArray is (or appears to be) dynamicAutoIt arrays are dynamic - as long as you ask them to be. Look at ReDim in the Help file. _FileListToArray uses this function to resize the returned array as required.Incidentally, using Dim to declare an array is not recommended - unless you want to erase all the elements of an existing array. It is better practice to use Global/Local as with normal variables.I hope that clears up the questions you had.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
martin Posted October 14, 2009 Posted October 14, 2009 (edited) I did some more experimenting as you suggested. $aFile is NOT an array after it is declared, but it IS an array after calling _FileReadToArray. Yes I know that, that is what I told you so I think you misunderstood. Please reread my post The code I posted works fine. It takes a text file that has 8904 lines and puts each line into an array element. Then I compare the arrays and single out the lines that are different. Again, as I said "Your code might work because _FileReadToArray will simply convert the variable you pass ByRef into an array,..." The word "might" was used becuase it looked to me like it should work but I didn't try it. I looked at the function code for _FileReadToArray, and could not see how the array was loaded. As far as I can tell, there is no loop in the code. And I am very curious as to why the arrays I declare are static, but the array inside _FileReadToArray is (or appears to be) dynamic? Can anyone help? As said, the function _FileReadtoArray converts the variable to an array. Suppose you have variable $a and $b Global $a $a = "bannanas" $b = 987 ConsoleWrite("$a and $b = " & $a & ', ' & $b & @CRLF) ;will show what you expect. ConsoleWrite("$a is an array? " & (IsArray($a)=True) & @CRLF) ;will show it is not an array, again as expected. ;Now make $a the return from a function which produces an array $a = StringSplit("a,b,c,d,e,f",',') ConsoleWrite("After assigning $a to stringsplit $a is an array? " & (IsArray($a) = True) & @CRLF) for $n = 1 to $a[0] ConsoleWrite($a[$n] & @CRLF) next This is what happens in the function _FileToArray. Just study the function and you will see what happens, no loop is needed. You need to play around with arrays a bit more. I don't really know what you mean by the variable you declare as being static. Since you declare in global space I would simply call it Global. My experience of static variables is in C where you can have a static variable declared in a function but which is retained in memory and the last value set is used the next time the function is called, but it is only available in that function. AutoIt does not have such a variable type and you would have to use a Global variable, or pass a variable using ByRef for example. EDIT: AutoIt does have static variables as of Beta version 3.3.1.4 26th October 2009. Edited November 7, 2009 by martin Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
cpt2400 Posted October 14, 2009 Author Posted October 14, 2009 Yes I know that, that is what I told you so I think you misunderstood. Please reread my post Again, as I said "Your code might work because _FileReadToArray will simply convert the variable you pass ByRef into an array,..." The word "might" was used becuase it looked to me like it should work but I didn't try it. As said, the function _FileReadtoArray converts the variable to an array. Suppose you have variable $a and $b Global $a $a = "bannanas" $b = 987 ConsoleWrite("$a and $b = " & $a & ', ' & $b & @CRLF) ;will show what you expect. ConsoleWrite("$a is an array? " & (IsArray($a)=True) & @CRLF) ;will show it is not an array, again as expected. ;Now make $a the return from a function which produces an array $a = StringSplit("a,b,c,d,e,f",',') ConsoleWrite("After assigning $a to stringsplit $a is an array? " & (IsArray($a) = True) & @CRLF) for $n = 1 to $a[0] ConsoleWrite($a[$n] & @CRLF) next [code=auto:0] This is what happens in the function _FileToArray. Just study the function and you will see what happens, no loop is needed. You need to play around with arrays a bit more. I don't really know what you mean by the variable you declare as being static. Since you declare in global space I would simply call it Global. My experience of static variables is in C where you can have a static variable declared in a function but which is retained in memory and the last value set is used the next time the function is called, but it is only available in that function. AutoIt does not have such a variable type and you would have to use a Global variable, or pass a variable using ByRef for example. I want to thank everyone for helping me understand. I will spend more time studying the help file, and these replies. Just to clarify this last response, I was speaking of static arrays, not static variables. Once again, thanks. And watch out, I think I have learned just enough to be dangerous.
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