Perl Array Length



I’ll examine below the Perl array length, by which I mean the length in bytes for a Perl array. Secondly, I want to find the memory usage of a Perl array variable.


Click Below to See the Best

Perl How-to Snippet Collection

and Save Hours of Surfing on Internet!


For computing the Perl array length I’ll use the length function which returns the number of characters of an expression. If you’ll use Unicode characters in your arrays, the number of characters may be different from the number of bytes. Let’s examine the example below:

#!/usr/bin/perl

use warnings;
use strict;

my @colors = (["blue", "red", "green"],
                     ["lightgreen", "lightblue"]);

# using the length() function
{
  use bytes;
  my $count = 0;

  foreach my $item1 (@colors){
    foreach my $item2 (@{$item1}){
      $count += length($item2); 
    }
  }
  print "Perl array length (through length() function) is: ", 
      $count, "\n";
}

The @colors is a 2-dimensional array and the snippet code shown above will get us the length in bytes of the @colors array. I used the use bytes pragma, and I put the snippet code in a block {} in order to limit the effect of this pragma to the scope in which it appears, i.e. inside the block. I ran the code on a Windows machine, Perl version 5.8.8 and I got the output: " Perl array length (through length() function) is: 31". This is one way of finding out how big the array is in bytes, but this is not the real memory usage of the array Perl variable.

Next I’ll examine the real length of a Perl array, i.e. the memory usage by that Perl array variable. To allocate an array, Perl uses a contiguous chunk of memory and you must worry about memory issues only in the case you are using large amounts of data. Perl does not limit the amount of storing bytes for a variable, it depends only on the amount of memory that’s available to your process.

Generally speaking, it’s not your task to deallocate the memory used by Perl arrays. If your variables are of my() type, Perl will try to free their storage when they go out of scope.

If you have a global array variable which uses a large amount of bytes for storing it, you can reuse the memory by using undef or delete functions. But, in this last case, the memory used by this variable is fragmented meantime the execution of Perl program, so if you re-allocate it, Perl will use another contiguous chunk of memory for your variable. After some free-allocate process, the memory used by the system will fragment progressively and you could end with some memory issues.

But let’s return to our intention to find out how much memory is used by a Perl array variable. A quick search through the CPAN modules and will get the Devel::Size module which will give us some help to solve this task. You can use this module to determine the memory usage of any Perl variable, including multidimensional arrays.

See the example below to find out how to use the Devel::Size module for getting the Perl array length used by OS to store that variable. Please note that the results will be dependent on your system and your architecture. Of course, in order to test this code on your machine, a prerequisite is to have the Perl Devel::Size module installed.

#!/usr/bin/perl

use warnings;
use strict;

my @colors = (["blue", "red", "green"], 
                    ["lightgreen", "lightblue"]);

# import the total_size function
use Devel::Size qw(size total_size);

my $develSize = size(\@colors);
print "Perl array length (through devel::size module) is : ",
       $develSize, "\n";


I used the total_size function to get the amount of memory used by @colors array. When executing, this function follows all the array references, so if we have in view a multidimensional Perl array, the result will be as accurate as possible. I ran this code on a Windows computer, Perl v5.8.8 and I got the output: "Perl array length (through devel::size module) is : 76".

Please compare the results obtained through the two methods for the same array and note that they have different significances.

If you want to dig deeper in the Perl memory allocation, a good idea it is to look for it in Devel::Size module documentation (http://search.cpan.org/~tels/Devel-Size-0.71/lib/Devel/Size.pm).

A-N-Y-O-N-E Can Learn and Master Perl!
And That Includes YOU!


Check these how-to tutorial eBooks (PDF format):


Table of Contents:

A Perl Script
Install Perl
Running Perl
Perl Data Types
Perl Variables
Perl Operators
Perl Lists
Perl Arrays
    Array Size
    Array Length (more)
Perl Hashes
Perl Statements
    Perl if
    Perl unless
    Perl switch
    Perl while
    Perl do-while
    Perl until
    Perl do-until
    Perl for
    Perl foreach
Built-in Perl Functions
    Functions by Category
        String Functions
        Regular Expressions and Pattern Matching
        List Functions
        Array Functions
        Hash Functions
        Miscellaneous Functions
    Functions in alphabetical order
        chomp
        chop
        chr
        crypt
        defined
        delete
        each
        exists
        grep
        hex
        index
        join
        keys
        lc
        lcfirst
        length
        map
        oct
        ord
        pack
        pop
        push
        q
        qq
        qw
        reverse
        rindex
        scalar
        shift
        sort
        splice
        split
        sprintf
        substr
        tr
        uc
        ucfirst
        undef
        unpack
        unshift
        values

return from Perl Array Length to Perl Basics



Would you like to create your own website like this one?
Hit the Alarm Clock!

Site Build It!