yutt Posted September 10, 2005 Share Posted September 10, 2005 How do you find out how many elements are in an array? I thought Ubound($array) would do this, but it just returns the dimensions of the array. I could make my own function... but I can't believe I'm the only one who ever needed such a function. I've searched the forums and help file for this for a long time, because I know there should be a simple solution. It is a waste of energy to be angry with a man who behaves badly, just as it is to be angry with a car that won't go. - Bertrand Russell Link to comment Share on other sites More sharing options...
Valuater Posted September 10, 2005 Share Posted September 10, 2005 (edited) $array[0] contains the amount of arrays msgbox(0,"", $array[0]) 8) Edited September 10, 2005 by Valuater Link to comment Share on other sites More sharing options...
yutt Posted September 10, 2005 Author Share Posted September 10, 2005 $array[0] contains the amount of arraysmsgbox(0,"", $array[0])8)<{POST_SNAPBACK}>Is that mentioned anywhere in the help file? It is a waste of energy to be angry with a man who behaves badly, just as it is to be angry with a car that won't go. - Bertrand Russell Link to comment Share on other sites More sharing options...
Valik Posted September 10, 2005 Share Posted September 10, 2005 $array[0] contains the amount of arraysmsgbox(0,"", $array[0])8)<{POST_SNAPBACK}>Really?Local $array[4] $array[0] = "This is not the size" $array[1] = "Other" $array[2] = 32 $array[3] = 0 MsgBox(4096, "", $array[0])Does that print the size of the array?yutt, if you are talking about finding the total number of elements in a multi-dimensional array, that is something you will have to enumerate using math and UBound() to get the sizes of the various dimensions. Link to comment Share on other sites More sharing options...
Valuater Posted September 10, 2005 Share Posted September 10, 2005 (edited) i think array display has the best info from help ; Ex. #1: #include <Array.au3> $asControls = StringSplit( WinGetClassList( "", "" ), @LF ) _ArrayDisplay( $asControls, "Class List of Active Window" ) Exit ; Ex. #2: #include <Array.au3> Dim $avArray[8] $avArray[0] = 7 $avArray[1] = "Brian" $avArray[2] = "Jon" $avArray[3] = "Larry" $avArray[4] = "Christa" $avArray[5] = "Rick" $avArray[6] = "Jack" $avArray[7] = "Gregory" _ArrayDisplay( $avArray, "_ArrayDisplay() Test" ) Exit ...very much like valik wrote above 8) Edited September 10, 2005 by Valuater Link to comment Share on other sites More sharing options...
yutt Posted September 10, 2005 Author Share Posted September 10, 2005 Hmm... from the example, general practice is to manually update the array size count in element 0? I suppose I can get in that habit, I just assumed the language would have some built-in system for figuring it out. It is a waste of energy to be angry with a man who behaves badly, just as it is to be angry with a car that won't go. - Bertrand Russell Link to comment Share on other sites More sharing options...
Gigglestick Posted September 10, 2005 Share Posted September 10, 2005 Hmm... from the example, general practice is to manually update the array size count in element 0?I suppose I can get in that habit, I just assumed the language would have some built-in system for figuring it out.<{POST_SNAPBACK}>Just use the array management functions for all your one-dimensional array needs and you can always use the $array[0] syntax. Much easier that way. My UDFs: ExitCodes Link to comment Share on other sites More sharing options...
Valuater Posted September 10, 2005 Share Posted September 10, 2005 (edited) Actually the reason i said $array[0] is from stringsplit()... this splits a string into an array help states Returns an array, the first element ($array[0]) contains the number of strings returned examples are $days = StringSplit("Sun,Mon,Tue,Wed,Thu,Fri,Sat", ",") ;$days[1] contains "Sun" ... $days[7] contains "Sat" for $x = 1 to $days[0] MsgBox(0,"", $days[$x]) Next sleep(100) $text = "This\nline\ncontains\nC-style breaks." $array = StringSplit($text, '\n', 1) for $x = 1 to $array[0] MsgBox(0,"", $array[$x]) Next MsgBox(0,"", "count1 = " & $days[0] & " " & "count2 = " & $array[0] & " ") notice both use for $x = 1 to $array[0] hope that helps 8) Edited September 10, 2005 by Valuater Link to comment Share on other sites More sharing options...
LxP Posted September 10, 2005 Share Posted September 10, 2005 I thought Ubound($array) would do this, but it just returns the dimensions of the array.<{POST_SNAPBACK}>When you declare an array of five elements, that array then has five elements whether or not you fill those elements with values.I think you might be referring to the number of used elements, in which case you would need to either keep count of how many elements you have written, or write code to work this out (giving it the logic to determine whether or not an element is 'unused' depending on its content). Link to comment Share on other sites More sharing options...
yutt Posted September 10, 2005 Author Share Posted September 10, 2005 (edited) Thanks for your help everyone. [inc|Dec]rementing the $array[0] works and is easy enough. Edit: Yeah, I did mean *used* elements. Edited September 10, 2005 by yutt It is a waste of energy to be angry with a man who behaves badly, just as it is to be angry with a car that won't go. - Bertrand Russell Link to comment Share on other sites More sharing options...
Valik Posted September 10, 2005 Share Posted September 10, 2005 (edited) Hmm... from the example, general practice is to manually update the array size count in element 0?I suppose I can get in that habit, I just assumed the language would have some built-in system for figuring it out.<{POST_SNAPBACK}>That is NOT the general practice. UBound() is used to get the size of the current dimension and that is the general practice. Some built-in functions put the size of the array in the first element because at the time (in the early days), UBound() did not exist so it was otherwise impossible to know the size of an array. Unfortunately we can't change the behavior now so we have a confusing situation where people incorrectly think the size of any array is at index 0.If you want to know the number of elements in a specific dimension, use UBound($array, $dimension) form of UBound. If you want to the total number of elements in an array, use this function which calculates the number of dimensions and then multiplies the size of each dimension found to get the total number of elements:Func _ArrayElementCount(ByRef $a) If Not IsArray($a) Then SetError(1) Return 0 EndIf Local $n = 1 For $i = 1 To UBound($a, 0) $n = $n * UBound($a, $i) Next Return $n EndFuncDemo:Local $array[40][20][30] Local $array2[5][2][2] Local $n1 = _ArrayElementCount($array) Local $n2 = _ArrayElementCount($array2) MsgBox(4096, "", "n1: " & $n1 & @CRLF & "n2: " & $n2)Edit: I see you finally provided all the information necessary in your edit above to understand the problem so you can basically disregard what I said since you aren't interested in available elements, just used elements. Edited September 10, 2005 by Valik Link to comment Share on other sites More sharing options...
Valuater Posted September 10, 2005 Share Posted September 10, 2005 still...a good demo Valik 8) 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