Friday, August 8, 2008

Count number of elements in array - PHP, Perl, Python

We often need to count the number of elements in an array (or get the size of an array). Here is how to do it in PHP, Perl & Python:

PHP: $items = count($ara);

Perl: my $items = scalar(@ara);, you can also try: my $items = @ara;

Python: items = ara.__len__(). Note that in Python it's called list. List can be used as array.

1 comment:

Paddy3118 said...

In Python, you would use:
items = len(myarray)


- Paddy.