notta Posted March 13, 2007 Share Posted March 13, 2007 Can you guys answer a few simple questions regarding the following code? #include <file.au3> Dim $aRecords If Not _FileReadToArray("c:\results.txt", $aRecords) Then MsgBox(4096, "Error", " Error reading log to Array error:" & @error) Exit EndIf ;1 msgbox(0, "", $aRecords[0]) ;2 Dim $aNew[$aRecords[0] + 1] ;3 MsgBox(0, "", $aNew[0]) for $i = 1 to $aRecords[0] $aNew[$i] = StringTrimRight($aRecords[$i], 4) Next ;4 MsgBox(0, "", $aNew[0]) _FileWriteFromArray("extstripped.txt", $aNew) I'm basically trying to understand arrays, so if you can, don't just post a better way to do what I'm trying to do. Let's say c:\results.txt has 500 lines of the format xxxxx.log. I'm stripping the extension off of the file name. ;1 The first msgbox, as I expected, outputs 500 ;2 I don't understand this. If I use $aNew[$aRecords[0]] than I receive a subscript error, so I have to add the + 1. Is it because of the 500 elements plus $aRecords[0] = 501? ;3 The second message box outputs nothing. I thought for sure this would output 500 as well. Why is $aNew[0] coming back blank? The for loop works fine. ;4 I was thinking maybe the array had to be completed first and now $aNew[0] would output 500, but again it comes back blank. What am I missing here? Lastly, when I look at the contents of extstripped.txt the first line is blank. Why is it skipping the first line and writing on the second? It ouputs all 500 lines fine, but now my line count shows 501 because of the empty first line. I feel I'm getting this, but still a little fuzzy. Thanks. Link to comment Share on other sites More sharing options...
Moderators big_daddy Posted March 13, 2007 Moderators Share Posted March 13, 2007 ;1 The first msgbox, as I expected, outputs 500From the help file: $aArray[0]will contain the number of records read into the array. ;2 I don't understand this. If I use $aNew[$aRecords[0]] than I receive a subscript error, so I have to add the + 1. Is it because of the 500 elements plus $aRecords[0] = 501?You are correct. ;3 The second message box outputs nothing. I thought for sure this would output 500 as well. Why is $aNew[0] coming back blank?As stated above the function itself sets this value. If you would like to set this manually you could use something like this: $aNew[0] = Ubound($aNew) - 1 Lastly, when I look at the contents of extstripped.txt the first line is blank. Why is it skipping the first line and writing on the second? It ouputs all 500 lines fine, but now my line count shows 501 because of the empty first line.The third parameter of _FileWriteFromArray() specifies the array index to start reading from. In your case this would be 1. Link to comment Share on other sites More sharing options...
SadBunny Posted March 13, 2007 Share Posted March 13, 2007 ;1 The first msgbox, as I expected, outputs 500 ;2 I don't understand this. If I use $aNew[$aRecords[0]] than I receive a subscript error, so I have to add the + 1. Is it because of the 500 elements plus $aRecords[0] = 501? ;3 The second message box outputs nothing. I thought for sure this would output 500 as well. Why is $aNew[0] coming back blank? The for loop works fine. ;4 I was thinking maybe the array had to be completed first and now $aNew[0] would output 500, but again it comes back blank. What am I missing here? Lastly, when I look at the contents of extstripped.txt the first line is blank. Why is it skipping the first line and writing on the second? It ouputs all 500 lines fine, but now my line count shows 501 because of the empty first line. I feel I'm getting this, but still a little fuzzy. Thanks.@1) OK. @2) Yes, you get a subscript later on in the script. This is because the 500 lines were read into a 501 elements array. ([0] - [500] = 501 elements, and Dim $array[500(=amount of lines read, see FileReadToArray help)] gives you an array of 500 elements, running from [0]-[499].) @3) You only Dim'd (Dimensioned) an array of 500 elements, but you put nothing in it. Hence, the [0] element is still empty. But I think I know where your misunderstanding comes from: the number is put in the [0] element specifically by the FileReadToArray function! Meaning, it is NOT by default containing the number of elements! You can use UBound() for that (see help). @4) This should be clear after @3). @Lastly) Your for loop filling in all the $aNew elements runs from 1 to 500, but the array $aNew itself has, again, 501 elements! (Including the [0].) Now, writing the array $aNew to a file writes all those 501 elements to a separate line, INCLUDING the [0] element which you did not fill yet since the for only goes from 1 to 500, so the [0] elements being empty results in an empty line. If you let your for loop run from 0 to 500 and change nothing else, (meaning all 501 $aNew elements would get filled), you WOULD get the 500 on the first line (except that you strimgtrim it 4 chars, to "500" would be all stringtrimmed away so the first line would still be empty, but you should know what I mean now ). Hope this helps you. Roses are FF0000, violets are 0000FF... All my base are belong to you. Link to comment Share on other sites More sharing options...
SadBunny Posted March 13, 2007 Share Posted March 13, 2007 Dammit I shouldn't go to the toilet and take phonecalls halfway during writing my post... Well, at least having two very similar explanations but in other words can't hurt Roses are FF0000, violets are 0000FF... All my base are belong to you. Link to comment Share on other sites More sharing options...
notta Posted March 13, 2007 Author Share Posted March 13, 2007 Nice explanations guys. Both posts cleared everything up perfectly. Thanks guys. 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