in reply to Re: Anonymous Reference???
in thread Anonymous Reference???

"An array can ONLY contain scalar elements."

Strictly speaking, you were not 100% correct as the following code is valid:

use Data::Dumper; use strict; use warnings; my @a = ((1,2,3), (4,5,6)); print $a[3];

So array can contain array. However Perl "flattens" the inner arrays, and does not remember the boundary information. This made it impossible to use this way to represent multi-dimentional array, and the array of array ref comes to rescue.

Update: Yes, the example sk given in his reply demos my point better.

However I disagree with his conclusion. I don't think either side is really wrong, and it really depends on whether you look at it from a syntax point of view (my view) or an internal represent view.

Replies are listed 'Best First'.
Re^3: Anonymous Reference???
by chromatic (Archbishop) on Sep 12, 2005 at 04:24 UTC

    This is incorrect. In this code:

    my @a = ((1,2,3), (4,5,6));

    ... there is only one array: @a. Everything else is a list. Lists are not arrays; lists and arrays behave very differently in various contexts, for example.

    The parentheses in the expression are for grouping to change the precedence of the right-side of the assignment, not to create lists (and especially not to create arrays).

      From this thread Scalars, Lists, and Arrays guess that is called a list expression and not a list itself. :)

      I think pg's example could have been something like this -  my @a = (1,2,3); my @b = (@a,4,5,6); here we use an array a in creating another array b. I think this just demonstrates that one can use arrays to create other arrays (you don't always need scalars separated by commas)...However I would not use the phrase arrays can contain arrays because once the array a is flattened under the list context the final array b just contains scalars

      In short I agree with halley.

Re^3: Anonymous Reference???
by halley (Prior) on Sep 12, 2005 at 12:31 UTC
    pg writes: I don't think either side is really wrong, and it really depends on whether you look at it from a syntax point of view (my view) or an internal represent view.

    Syntax is only useful insofar as it develops a data structure. Data structures are what enable algorithms. Following all of perl's "there is more than one way to phrase it" syntax eccentricities will just confuse you; conversely, recognizing perl's very few, very simple, very self-consistent data structures will let you accomplish many algorithms with few errors borne of such confusion.

    • a scalar is O(1) in storage; undef, int, real, string, or reference;
      sometimes there are cached alternative representations but it's still O(1)
    • an array contains a known number, zero or more, of scalars
    • a hash associates zero or more pairs; each pair is a unique string key and an arbitrary scalar value
    • (globs are public implementation details of the machine, skipped for this discussion)

    Lists have no name and no known length. You can't take $#list or $list[-1] or even $list[3] without a conversion to array first. Lists are not a first-class storage data structure, but are the product of processing the syntax of literals, or the product of iteration.

    --
    [ e d @ h a l l e y . c c ]