Counting the number of elements in an XMLList : Actionscript and Python are more alike than I thought.

I just realized that ActionScript 3.0 and Python are more alike than I thought.

When I started programming my very very first program in MXML and ActionScript (not too long ago, actually), I had written a nice and short function that counts the number of elements in an XMLList variable, of which I was very proud. After all, it worked !

// Function to count all elements and return the total count in a given XMLList
// Warning : using XML will return always 1, use XMLList instead
private function countElements(list:XMLList):int{
var counter:int = 0;
for each (var prop:XML in list){
counter += 1;
}
return counter;

When all I *really* needed was to state

// Duh!
var counter:int = XMLfile.length();

I should have known something like this existed ! Python has similar ‘methods’ available, although for a list they are called as function ‘len’ :

counter = len(list)

Oh well, live and learn.

Leave a Reply

Your email address will not be published. Required fields are marked *