Neo,

The primary difference is that, the first one is an array of 3 elements and 
second is an array containing one element which in turn is a reference (an 
anonymous array).

This is probably best explained by an example.

[code]
use strict;
use warnings;
use Data::Dumper qw/Dumper/;

my @a1 = ( 1, 2, 3 );
print '@a1: ' . Dumper( \@a1 );
print 'The length of the array @a1 is : ', scalar @a1, "\n";

my @a2 = [ 1, 2, 3 ];
print '@a2: ' . Dumper( \@a2 );
print 'The length of the array @a2 is : ', scalar @a2, "\n";

my $a3 = [ 1, 2, 3 ];
print '$a3: ' . Dumper( $a3 );
print 'The length of the array $a3 is : ', scalar @$a3, "\n";

my @a4 = ( 1, 2, 3 );
my $a4 = \@a4;
print '$a4: ' . Dumper( $a4 );
print 'The length of the array $a4 is : ', scalar @a4, "\n";
[/code]

[output]
@a1: $VAR1 = [
          1,
          2,
          3
        ];
The length of the array @a1 is : 3
@a2: $VAR1 = [
          [
            1,
            2,
            3
          ]
        ];
The length of the array @a2 is : 1
$a3: $VAR1 = [
          1,
          2,
          3
        ];
The length of the array $a3 is : 3
$a4: $VAR1 = [
          1,
          2,
          3
        ];
The length of the array $a4 is : 3
[/output]
 
best,
Shaji 
-------------------------------------------------------------------------------
Your talent is God's gift to you. What you do with it is your gift back to God.
-------------------------------------------------------------------------------


________________________________
 From: Neo Anderson <neo_in_mat...@msn.com>
To: beginners@perl.org 
Sent: Tuesday, 8 January 2013 7:03 AM
Subject: What is the difference between () and [] syntax for array?
 

I think there are two ways to declare an array:
my @a1 = (1, 2, 3);my @a2 = [1, 2, 3];
What is the difference?                           

Reply via email to