$pair[0]?

0 views
Skip to first unread message

Ingo Blechschmidt

unread,
Aug 4, 2005, 5:49:34 PM8/4/05
to perl6-l...@perl.org
Hi,

my $pair = (a => 1);
say $pair[0]; # a?
say $pair[1]; # 1?

I've found this in the Pugs testsuite -- is it legal?


--Ingo

--
Linux, the choice of a GNU | Black holes result when God divides the
generation on a dual AMD | universe by zero.
Athlon! |

Andrew Shitov

unread,
Aug 4, 2005, 6:22:33 PM8/4/05
to perl6-l...@perl.org
> say $pair[0]; # a?

It looks like $pair is an arrayref while 'say ref $pair' tells 'Pair'.

And may I ask a relating question:

my $pair = ('name' => 'age');
say $pair{'name'}; # prints 'age'
say $pair['name']; # why prints 'name'? <== question
say $pair['age']; # prints 'name'


--
___________________________________________________________________________
Andrew, an...@shitov.ru
___________________________________________________________________________

Ingo Blechschmidt

unread,
Aug 4, 2005, 6:27:53 PM8/4/05
to perl6-l...@perl.org
Hi,

Andrew Shitov wrote:
>> say $pair[0]; # a?
>
> It looks like $pair is an arrayref while 'say ref $pair' tells 'Pair'.

right, this is why I asked, IMHO it's bogus.

> And may I ask a relating question:
>
> my $pair = ('name' => 'age');
> say $pair{'name'}; # prints 'age'
> say $pair['name']; # why prints 'name'? <== question
> say $pair['age']; # prints 'name'

That's probably because both "name" and "age" get numified to 0 which in
turn means (in current Pugs) .key.


--Ingo

--
Linux, the choice of a GNU | There are no answers, only
generation on a dual AMD | cross-references.
Athlon! |

Luke Palmer

unread,
Aug 4, 2005, 6:52:24 PM8/4/05
to Ingo Blechschmidt, perl6-l...@perl.org
On 8/4/05, Ingo Blechschmidt <ibl...@web.de> wrote:
> Hi,
>
> my $pair = (a => 1);
> say $pair[0]; # a?
> say $pair[1]; # 1?
>
> I've found this in the Pugs testsuite -- is it legal?

Nope. That's:

say $pair.key;
say $pair.value;

Also:

say $pair<a>; # 1
say $pair{anything else}; # undef

But we don't implicitly cast references like that.

Luke

Ingo Blechschmidt

unread,
Aug 4, 2005, 7:04:43 PM8/4/05
to perl6-l...@perl.org
Hi,

thanks for clarification, that's what I've thought, too :)


--Ingo

--
Linux, the choice of a GNU | The next statement is not true.
generation on a dual AMD | The previous statement is true.
Athlon! |

Larry Wall

unread,
Aug 5, 2005, 2:36:16 PM8/5/05
to perl6-l...@perl.org
On Fri, Aug 05, 2005 at 12:27:53AM +0200, Ingo Blechschmidt wrote:
: Hi,

:
: Andrew Shitov wrote:
: >> say $pair[0]; # a?
: >
: > It looks like $pair is an arrayref while 'say ref $pair' tells 'Pair'.
:
: right, this is why I asked, IMHO it's bogus.

Yes, for bare pairs, it's probably somewhat bogus. But now I'm asking
myself about the use of as a Lispish '.':

'a' => 'b' => 'c' => 'd'

There's something to be said for having a way of indexing into that
using numeric subscripts. Certainly Lisp's extensible car/cdr notation
is the wrong way to do it, but cdddr is certainly shorter than

$pair.value.value.value

But maybe that's worth being dehuffmanized like that...

Larry

Mark Reed

unread,
Aug 5, 2005, 4:30:25 PM8/5/05
to Yuval Kogman, perl6-l...@perl.org
Seems like you left out the degenerate case for when you run out of pairs:

sub infix:<!!> (Scalar $x, 0) { $x }

On 2005-08-05 16:24, "Yuval Kogman" <nothi...@woobling.org> wrote:

> On Fri, Aug 05, 2005 at 11:36:16 -0700, Larry Wall wrote:
>
>> There's something to be said for having a way of indexing into that
>> using numeric subscripts. Certainly Lisp's extensible car/cdr notation
>> is the wrong way to do it, but cdddr is certainly shorter than
>>
>> $pair.value.value.value
>>
>> But maybe that's worth being dehuffmanized like that...
>

> Haskell has !! :
>
> sub infix:<!!> (Pair $x, 0) { $x.key }
> sub infix:<!!> (Pair $x, Int $index) { $x.value !! ($index - 1) }


Yuval Kogman

unread,
Aug 5, 2005, 4:24:36 PM8/5/05
to perl6-l...@perl.org
On Fri, Aug 05, 2005 at 11:36:16 -0700, Larry Wall wrote:

> There's something to be said for having a way of indexing into that
> using numeric subscripts. Certainly Lisp's extensible car/cdr notation
> is the wrong way to do it, but cdddr is certainly shorter than
>
> $pair.value.value.value
>
> But maybe that's worth being dehuffmanized like that...

Haskell has !! :

sub infix:<!!> (Pair $x, 0) { $x.key }
sub infix:<!!> (Pair $x, Int $index) { $x.value !! ($index - 1) }

--
() Yuval Kogman <nothi...@woobling.org> 0xEBD27418 perl hacker &
/\ kung foo master: /me supports the ASCII Ribbon Campaign: neeyah!!!

Reply all
Reply to author
Forward
0 new messages