Perl Array Size

These sites are FOR SALE: miscperlinfo.com, mpihowto.com!
If you are interested, please use my Contact page.




If we speak about Perl array size we mean the number of elements of a Perl array. Please note that some authors don’t make any difference between size and length, but we’ll try to be consequent and use the word "size" when we speak about the number of Perl array items. We’ll discuss below the two possible array alternatives: one-dimensional and the other multi-dimensional.

One-dimensional arrays

If you want to get the Perl array size (or the number of its elements), you can use each of the code lines shown below:

$arraySize = @array;
$arraySize = scalar (@array);
$arraySize = $#array + 1;

The first line of code uses the implicit scalar conversion, meanwhile the second line uses the explicit scalar conversion.

In the third line of code, $#array means the subscript (or index) of the last item of the array and you must add 1 in order to obtain the array number of elements.


Click Below to See the Best

Perl How-to Snippet Collection

and Save Hours of Surfing on Internet!


If you want to print the Perl array size, see the code below:

print "array size = $arraySize\n";
print "array size = ", @array. "\n";
print "@array";

The first line prints the scalar variable $arraySize which contains the size of the array.

The second line prints the size of the array using directly its name.

The last line is a wrong example about how to use the name of an array to get the Perl array size. Including the array name in double quotes causes Perl to flatten the array by concatenating its elements into a string and the print function will display all the elements of the array separated by space, and not its size.

And finally, don’t use the length function to get the Perl array size. See the example code block below:

@words = qw(Perl array size);
$size = length(@words);
print "$size\n"; 

The last line will print 1 because the length function expects a scalar and the array will be consider in a scalar context – so will get the length of the length. Thus, in scalar context the notation @words means 3, and length(3) is equal to 1.

Multi-dimensional arrays

Next, we will show you how you can get the Perl array size for multi-dimensional arrays. If you remember the definition of an array, you know that an array contains a list of scalars. Because an array is not a scalar, if we include an array into another array, Perl uses the reference of the included array and not the array itself. In order to get access to an array included in another array we must dereference the array reference – we can use for this a block in curly braces, as you’ll see in the print examples shown below.

@numbersTwo = ( [1, 2, 3],  [4, 5, 6, 7], [1], 
                         [2, 5, 8, 6, 3] );
@numbersThree = ( [ [1, 3, 5, 7, 9] ],
                  [ [2, 4, 6, 8],  [22, 24], [26], 
                         [28, 30, 32] ] );

In the above example, the @numbersTwo array has two dimensions and @numbersThree array has three dimensions. Practically, the @numbersTwo array has four elements: respectively the references to the [1, 2, 3] , [4, 5, 6, 7], [1] , [ 2, 5, 8, 6, 3] lists. If we want to print the numbers of elements of the @numbersTwo array (the Perl array size), we can use the code below:

print $#numbersTwo+1, "\n";

which will get us 4. If we want to print the numbers of elements of the second element of the @numbersTwo array, i.e. the list [4, 5, 6, 7], we can use the code:

print $#{$numbersTwo [1]}+1, "\n";

which will get us 4. For inner dimensions, for example if we want to print the Perl array size of the sub-array that contains the list [28, 30, 32] (included in the @numbersThree array) we can use the code below:

print $#{$numbersThree[1][3]}+1, "\n";

which will get us 3. See some other print examples below:

print $#{$numbersTwo[0]}+1, "\n";      # prints 3
print $#{$numbersTwo[1]}+1, "\n";      # prints 4
print $#{$numbersTwo[2]}+1, "\n";      # prints 1
print $#{$numbersTwo[3]}+1, "\n";      # prints 5

print $#{$numbersThree[0][0]}+1, "\n";      # prints 5
print $#{$numbersThree[1][0]}+1, "\n";      # prints 4
print $#{$numbersThree[1][1]}+1, "\n";      # prints 2
print $#{$numbersThree[1][2]}+1, "\n";      # prints 1
print $#{$numbersThree[1][3]}+1, "\n";      # prints 3

Finally, I’ll give you two samples of code about how you can print all the elements of a multi-dimensional Perl array, each subarray on a separate line:

# print @numbersTwo array

foreach $item1 (@numbersTwo){
  foreach $item2 (@{$item1}){
    print "$item2 ";  
  }
  print "\n";
}

# print @numbersThree array

foreach $item1 (@numbersThree){
  foreach $item2 (@{$item1}){
    foreach $item3 (@{$item2}){
      print "$item3 ";  
    }
    print "\n";
  }
}

If you want to see how to find the length of an array of arrays (an array that has as elements references to other arrays), I suggest you to click here to watch this video on youtube.


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 (more)
    Array Length
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 Size to Perl Basics



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

Site Build It!