On 01/08/2013 12:03 PM, Neo Anderson wrote:
> I think there are two ways to declare an array:
> my @a1 = (1, 2, 3);my @a2 = [1, 2, 3];
> What is the difference?                                         

The first declares an array with three elements 1, 2 and 3.

The second declares an array with 1 element, a (scalar) reference to a 3
element list.

See 'perldoc perldata' and 'perldoc perlref' for details of the syntax, and
maybe 'perldoc perldsc' (Data Structures Cookbook) for some worked examples
of complicated structures.

Run the following code to illustrate the difference:

#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper qw/ Dumper /;;

my @a1 = ( 1, 2, 3 );
print '@a1: ' . Dumper(\@a1);
my @a2 = [ 1, 2, 3 ];
print '@a2: ' . Dumper(\@a2);

Cheers,
Michael

-- 
Michael Brader                    Senior Software Engineer and Perl Person
                                  Technology/Softdev/M&E Small Change Team
Internode       http://internode.on.net/          mbra...@internode.com.au
iiNet             http://iinet.net.au/         m.bra...@staff.iinet.net.au


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to