in reply to Re: Hash initialization works, but why?
in thread Hash initialization works, but why?

The short answer is that @array{1,2,3} is a hash slice, and therefore an array

That's wrong. It a hash slice, and it produces a list. It's not an array. Very different.

  • Comment on Re^2: Hash initialization works, but why?

Replies are listed 'Best First'.
Re^3: Hash initialization works, but why?
by throop (Chaplain) on Jun 04, 2008 at 23:23 UTC
    I admit, I've always thought that 'array' was just Perl's particular jargon for 'list'. Or maybe "Perl's data-structure for holding a list." If I write
    @foo = (1,2,3)
    then @foo is an array, while (1,2,3) is a list. Right?

    I find myself still confused on the distinction between an array and a list. Could you explain more?

    throop

      It gets more confusing (but then it gets more clear).

      @foo is an array

      Yes.

      while (1,2,3) is a list

      1, 2, 3 is a list. The parentheses have nothing to do with its listishness. The parentheses are there only so that the parser doesn't parse the whole expression as:

      (@foo = 1), 2, 3;

      ... as it would if there were no grouping parentheses. They indicate which list you want to assign.

        "Parentheses...have nothing...to do with...its listishness."

        Ah. Understanding is slowly dawning to this refugee from 10 years of LISP programming.

        throop

      List and Array have very clear and distinct definitions in Perl.

      List is a type of value.
      Array is a type of variable.

      You can't take the length of a list.
      There's no such thing as a reference to a list.
      There's no such thing as a list in scalar context.

      Come to think of it, that's in the FAQ.

      One more tidbit. The variable @foo is an array, but the expression @foo evaluates to a scalar or a list, depending on context.
      my $num_elemens = @foo; # @foo evaluates to a scalar. my ($first, $second) = @foo; # @foo evaluates to a list.