develooper Front page | perl.perl6.language | Postings from May 2005

Re: (1,(2,3),4)[2]

Thread Previous | Thread Next
From:
Mark Reed
Date:
May 25, 2005 07:49
Subject:
Re: (1,(2,3),4)[2]
Message ID:
BEBA08B0.6233%mark.reed@turner.com
[1,2,3] is not an array or a list.  It is a reference to an anonymous array.
It is not 3 values; it¹s 1 value, which happens to point to a list of size
3.  If you assign that to an array via something like @a = [1,2,3], I would
expect at least a warning and possibly a compile-time error.

 If it does work, it probably gets translated into @a = ([1,2,3]), which
creates an array of size 1 whose first (and only) element is a reference to
an array of size 3.  That would give this result:

+@a == 1;

@a[0] == [1,2,3]; 
@a[1] == undef; 
@a[0][0] == 1;
@a[0][1] == 2; 
@a[0][2] == 3;

I¹m not sure about +(@a[0]), but I¹m guessing it would == 3.





On 2005-05-25 04:47, "TSa (Thomas Sandlaß)" <Thomas.Sandlass@orthogon.com>
wrote:

> Juerd wrote: 
>> > An array in scalar context evaluates to a reference to itself.
>> > 
>> > A hash in scalar context evaluates to a reference to itself.
>> > 
>> > An array in list context evaluates to a list of its elements.
>> > 
>> > A hash in list context evaluates to a list of its elements (as pairs).
>> > 
>> > Array context is a scalar context.
> 
> I have understand what you mean and how you---and other p6l'er---
> derive +@a == 1 from @a = [1,2,3]. But allow me to regard this
> as slightly inconsistent, asymmetric or some such.
> 
> Isn't hash context missing in the list above? How does
> 
> %a = ( a => 1, b => 2, c => 3 )  # @a = (1,2,3)
> 
> compare with 
> 
> %b = { a => 1, b => 2, c => 3 }  # @b = [1,2,3]
> 
> Does that mean 
> 
> 3 == +%a == +%b 
>    == +{ a => 1, b => 2, c => c }
>    == +( a => 1, b => 2, c => c )
> 
> holds and the access of the hash works as expected:
> 
> %a<a> == 1 == %b<a>  # and @a[0] == 1, but @b[0][0] == 1
> 
> What would actually be the equivalent syntax to @b?
> Is it %b<><a> or %%b<a> or even (*%b)<a>?
> It will hardly be %b{undef}<a>, though.



Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About