dave8775 has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

what is the difference between these declarations?

my(@a) = (); my($b) = {}; my(@c) = []; my(%d) = ();

It seems like we can build Hashes of hashes, array of hashes, record structures etc, either by using pointer syntax or some other way... I am getting somewhat confused on what is the best most efficient way.. All declarations above seem to work without any noticeable difference *shrug*.

David

Replies are listed 'Best First'.
Re: data types
by sauoq (Abbot) on Jan 11, 2003 at 01:14 UTC
    my(@a) = (); # This declares a lexically scoped array and initializes + # it to be empty. my($b) = {}; # This declares a lexically scoped scalar and initialize +s # it to a reference to an empty anonymous hash. my(@c) = []; # This is probably a mistake. # It declares a lexically scoped array and sets its # first element to a reference to an empty anonymous # array. my(%d) = (); # This declares a lexically scoped hash and initializes # it to be empty.
    -sauoq
    "My two cents aren't worth a dime.";
    
Re: data types
by Arien (Pilgrim) on Jan 11, 2003 at 01:25 UTC
    What is the difference between these declarations?
    my (@a) = (); # @a contains an empty list my ($b) = {}; # $b contains a ref to an empty (unnamed) hash my (@c) = []; # @c contains a ref to an empty (unnamed) array my (%d) = (); # %d contains an empty list

    You may want to (re-)read the documentation on basic data types and references and nested data structures.

    — Arien

Re: data types
by Wysardry (Pilgrim) on Jan 11, 2003 at 01:55 UTC

    Whether you use hashes or arrays (or nested versions of them) really depends on how you want to access and use the data.

    Hashes are referenced by a key, which is usually a word, whereas arrays are indexed by a number.

    Imagine for a moment that your program keeps track of data for a car race. You could print out who was in the lead by referencing an array called $positions like this:-

    print $position_driver[1];

    Mansell

    You could display what position a certain driver was in like this:-

    print $driver_position{'Schumaker'};

    2

    Which you use depends which makes more sense to you.

    You won't see much of a difference between them just from a declaration, you need to actually use them for that.

Re: data types
by Hofmator (Curate) on Jan 11, 2003 at 15:41 UTC
    As a sidenote, be aware that there is a difference between the following two cases:
    my @array = ('hello', 'blah', 'bye'); my($scalar1) = @array; # $scalar1 eq 'hello' my $scalar2 = @array; # $scalar2 == 3 print join ' ', $scalar1, $scalar2;

    The first works like a list assignment (list context) ($name, $last) = ($last, $name); assigning the first element of the array to $scalar1.

    The second puts @array into scalar context which returns the length of the array into $scalar2.

    -- Hofmator

Re: data types
by Coruscate (Sexton) on Jan 11, 2003 at 22:45 UTC

    The only explanation I see missing here is how you might build any of these data structures and how to access the data you put into it.

    # Declare an array and print out two elements my (@a) = qw( foo bar baz buz ); print join(", ", $a[0], $a[2]), "\n"; # Declare a reference to an anonymous hash and print # out some data from it my ($b) = { 'foo' => 'bar', 'baz' => 'baz' }; print join(", ", $b->{'foo'}, $$b{'baz'}), "\n"; # The following statement is quite odd. my (@c) = [ 'foo', 'bar', 'baz', 'buz' ]; # Perhaps you meant the following: my ($c) = [ 'foo', 'bar', 'baz', 'buz' ]; print join(", ", $c->[1], $$c[3]), "\n"; # Create a regular hash and print out some data: my (%d) = ( 'foo' => 'bar', 'baz' => 'buz' ); print join(", ", $d{'foo'}, $d{'baz'}), "\n";

    Note that in the 2nd and 3rd cases I presented, there are 2 methods of accessing the data within the structure. You can either double the dollar sign ($$b{'baz'} or $$c[3]) or use the dereferencing characters ($b->{'foo'} or $c->[1]).