jimmyjmmy Posted October 12, 2008 Share Posted October 12, 2008 Hi, How do I pre-declare an array with an unknown number of elements that it is going to hold. For example, when I read a textfile into an array, the number of elements in this text file is unknown to me and yet I need to pre-declare the array, so how do I pre-declare this array. In perl it is written as :- my @array = (); Thanks Link to comment Share on other sites More sharing options...
Mattraks Posted October 12, 2008 Share Posted October 12, 2008 #include <File.au3> Global $sFile = @ScriptDir & "\File.txt", $iLines = _FileCountLines($sFile) Dim $aFile[$iLines] For $i = 1 To $iLines $aFile[$i] = FileReadLine($sFile, $i) Next Link to comment Share on other sites More sharing options...
j00ls Posted October 12, 2008 Share Posted October 12, 2008 (edited) I have tried to use your code the other way around: $iArray = _ArrayGetLength($oFullArray) For $i = 1 To $iArray FileWriteLine($sFile, $oFullArray[$i]) Next and i get the following error: => Array variable has incorrect number of subscripts or subscript dimension range exceeded. i have no idea what else to try ( Edited October 12, 2008 by j00ls Link to comment Share on other sites More sharing options...
Mattraks Posted October 12, 2008 Share Posted October 12, 2008 Look into Ubound Link to comment Share on other sites More sharing options...
james3mg Posted October 12, 2008 Share Posted October 12, 2008 Dim it as an array with only one subscript to start, then just before you go to add another element, use ReDim to resize the array without destroying the previous contents. ...usually it'll be something like redim $iArray[ubound($iArray)+1] "There are 10 types of people in this world - those who can read binary, and those who can't.""We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true." ~Robert Wilensky0101101 1001010 1100001 1101101 1100101 1110011 0110011 1001101 10001110000101 0000111 0001000 0001110 0001101 0010010 1010110 0100001 1101110 Link to comment Share on other sites More sharing options...
j00ls Posted October 12, 2008 Share Posted October 12, 2008 thx i will give redim a try... i found a workaround solution ... but they allways come back to haunt you so I guess il try to fix it the way its supposed to be Link to comment Share on other sites More sharing options...
jimmyjmmy Posted October 12, 2008 Author Share Posted October 12, 2008 Please pardon me. So how do I create an empty array. Thanks Link to comment Share on other sites More sharing options...
liteswap Posted October 12, 2008 Share Posted October 12, 2008 Please pardon me. So how do I create an empty array. ThanksCheck the help file. Or Try Dim $aArray[1] <- single-dimensional array with one element numbered zero. Link to comment Share on other sites More sharing options...
james3mg Posted October 12, 2008 Share Posted October 12, 2008 Check the help file. Or Try Dim $aArray[1] <- single-dimensional array with one element numbered zero.Right...you can't Dim $aArray[0]...to be an array it has to have at least one subscript. And you can't redim a variable to make it into an array. So you could either have a special case for the first value found (like:if $iArray[0]<> "" OR UBound($iArray)>1 Then Redim $iArray[UBound($iArray)+1]; this will not redim the array if it has only one subscript and that subscript is empty ), or you could assign the value FIRST, always redim it one greater AFTER, and know that you'll always have one "extra" subscript and eliminate it at the end of your loop Redim $iArray[UBound($iArray)-1] Hope that helps. "There are 10 types of people in this world - those who can read binary, and those who can't.""We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true." ~Robert Wilensky0101101 1001010 1100001 1101101 1100101 1110011 0110011 1001101 10001110000101 0000111 0001000 0001110 0001101 0010010 1010110 0100001 1101110 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