Perl Basics   «Prev 

Perl Anonymous Arrays

You create an anonymous array by using square brackets like this:

$arrayref = [ 'one', 'two', 'three' ]; 

There exists an array that has no name but is available via the $arrayref reference, which is just like any other array reference in all other respects. You can easily create a two-dimensional array as follows.
$arrayref = [ 'one', 'two', [ 1, 2, 3 ], 'four' ];

That makes a reference to an anonymous array ($arrayref), which contains three scalars ('one', 'two', and 'four') and another anonymous array ([ 1, 2, 3 ]). You can dereference the values in the array like this:

$arrayref->[1]       # 'two'
$arrayref->[2][1]    # 2

You can even expand an anonymous array without any arbitrary restrictions.
$arrayref->[2][3] = 4;   # expand the nested array!

You now have an array that looks like this:
$arrayref = [ 'one', 'two', [ 1, 2, 3, 4 ], 'four' ];

References to Anonymous Storage

So far, we have created references to previously existing variables. Now we will learn to create references to "anonymous" data structures which are values that are not associated with a variable.
To create an anonymous array, use square brackets instead of parentheses:
$ra = [ ];  # line 1
$ra = [1,"hello"]; # line2
  1. Creates empty, anonymous array, returns a reference to it
  2. Creates an initialized anonymous array, returns a reference to it

This notation not only allocates anonymous storage, it also returns a reference to it, much as malloc(3) returns a pointer in C.
Question: What happens if you use parentheses instead of square brackets?
Recall again that Perl evaluates the right side as a comma-separated expression and returns the value of the last element;
$ra contains the value "hello", which is likely not what you are looking for.

Use cases for Perl anonymous arrays

Perl anonymous arrays are arrays that are created without assigning them to a named variable. They are useful in various scenarios where you need to create a temporary array, pass an array to a function, or store arrays within data structures like hashes or other arrays. Here are several use cases when you might use Perl anonymous arrays:
  1. Temporary arrays: When you need a temporary array for a short-lived operation, anonymous arrays can be useful. By not assigning a name to the array, you can create a concise and efficient representation.
    print join(", ", @{[1, 2, 3, 4, 5]}), "\n";
    
  2. Passing arrays to functions: Anonymous arrays can be used to pass an array to a function without creating a named array beforehand. This can make your code more concise and easier to read.
    sub print_array {
        my ($array_ref) = @_;
        print join(", ", @$array_ref), "\n";
    }
    
    print_array([1, 2, 3, 4, 5]);
    
  3. Storing arrays within arrays (multidimensional arrays): Anonymous arrays can be used to create multidimensional arrays by nesting them within other arrays. This allows you to represent more complex data structures.
    my $matrix = [
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]
    ];
    
  4. Storing arrays within hashes: When you want to associate arrays with keys in a hash, anonymous arrays can be used to create and store the arrays directly within the hash.
    my %grades = (
        "John" => [90, 85, 78],
        "Jane" => [95, 88, 92],
        "Mary" => [88, 90, 76]
    );
    
  5. Returning arrays from functions: Anonymous arrays can be used to return arrays from functions without having to create named arrays within the function.
    sub create_array {
        my ($start, $end) = @_;
        return [($start .. $end)];
    }
    
    my $array_ref = create_array(1, 5);
    print join(", ", @$array_ref), "\n";
    
    

These use cases demonstrate the versatility and convenience of Perl anonymous arrays in various situations, such as passing arrays to functions, creating temporary arrays, and storing arrays within data structures like hashes and other arrays.

Perl Complete Reference