Determining length and size

Determining length and size

Introduction

Quite often you will want to know the length or size of a variable. It may be a scalar variable, or you may want to know how many elements are in an array or a hash.

Size, or length, can mean a different thing depending on what context you mean. Generally it can mean the number of characters in a string, or the number of elements in an array or hash. However, if you are using Unicode characters the number of characters in a string may be different to the number of bytes in the string.

Example 1. length() and scalar variables

To determine the number of characters in an expression use the length() function:

This example prints the number of characters in the string $name:

Example 2. length() in bytes

Sometimes you want to know the number of bytes in the string, not the number of characters. This won’t affect you for ASCII characters but it may if you are using Unicode characters.

By default the length() function returns the number of characters. You can tell it to return the number of bytes by specifying use bytes;, as in this example:

This outputs:

Example 3. Arrays: last index

In Perl you can determine the subscript of the last element of an array and add 1 to it to get the number of elements in the array.

This gives us:

Example 4. Arrays: scalar context

If you use an array in scalar context, it will return the number of elements in the array:

This gives us:

Apart from being confusing to read, this method can lead to some easy mistakes. For example, consider the following program:

What would you expect it to print?

Many functions, double-quotes included, treat arrays differently to scalar variables. The double-quotes cause Perl to flatten the array by concatenating the values into a string.

If you want the number of elements in the array it is best to be explicit about it (see example 6 below).

Example 5. Arrays: don’t use length()

If you try to use the length() function on an array, it won’t give you the answer you are after:

The output is not what you would expect:

This is because the length() function expects a scalar, so the array is forced into scalar context.

As we saw above (example 4), an array in scalar context already gives us the length. The example above is giving us the length of the length: the length of 5 is 1.

Example 6. Arrays: using scalar()

While example 3 and 4 are correct, they aren’t very readable or friendly to use. Perl has the scalar() function which forces the array into scalar context, giving you the length:

This also gives us the correct answer:

Example 7. Number of elements in a hash

Sometimes you will also want to know the number of elements in a hash. This is easily done using the keys() function to return the keys as an list, and the scalar() function to return how many keys there are:

The output of this program is:

Example 8. Finding the memory usage of Perl variables

You can also determine the memory usage in bytes of any variable, including multi-dimensional structures (e.g. a hash of hashes, or an array of arrays, or array of hashes etc). To do this, use the Devel::Size module.

On our machine this outputs:

This answer is somewhat system dependent. To get a better understanding of how Devel::Size measures the size of variables, see perldoc Devel::Size.

See also

Scroll to Top