develooper Front page | perl.perl6.language.data | Postings from September 2000

Re: RFC 204 (v2) Arrays: Use list reference for multidimensional array access

From:
Jeremy Howard
Date:
September 25, 2000 17:24
Subject:
Re: RFC 204 (v2) Arrays: Use list reference for multidimensional array access
Message ID:
001e01c02750$1b6b2ed0$0100a8c0@jeremy
Buddha Buck wrote:
> The main problem I see are these:
>
> @array = ([1,2],[3,4]);  # 2-dimensional array, using LOL syntax
> print $array[[1,1]];     # prints 4, OK
> print $array[1];         # prints ?????
> print $array[[1,1,1]];   # prints ?????
> print $array[[1]];       # prints ?????
>
> According to RFC 204, $array[1] should return [3,4], $array[[1]] should
> return (3,4), and $array[[1,1,1]] should cause a runtime error.
>
> Interestingly enough, my original version of RFC204 had all three of
> the "questionable" print statements be runtime errors -- I don't like
> LOL-semantics for multidimensional arrays.  Somehow, it got changed
> when being passed from me through Jerry to the RFC Librarian.  I didn't
> bother to reread it after it got posted -- I knew what it said -- so I
> didn't notice the change.
>
Sorry Buddha--I thought that we were agreed on the "arrays are list of
lists" thing, so that I was just adding clarification. We can say in Perl 5:

  @array = ([1,2],[3,4]);  # 2-dimensional array, using LOL syntax
  print $array[1];

I don't think there's any sensible way of changing this to an error in Perl
6 (or any reason to, either). That's why I clarified that in your RFC. Then
there's the question of @array[[1]]. If that fails to deref automagically,
then 1d arrays and multidim arrays will suddenly have different interfaces
(since a 1d array doesn't contain refs, while n-dim arrays do).

>     $array[[1,2]] = (1,2,3,4); # Sets the line at (1,2) to (1,2,3,4)
>
> Huh?  That's a scalar on the right, a list on the left?  Should that
> set $array[[1,2]] to 4?
>
It should. The RFC should say:

  @array[[1,2]] = (1,2,3,4); # Sets the line at (1,2) to (1,2,3,4)

> > Puh. I'd prefer
> >
> > my @a = @$array[0];
> >
> > which is shorter anyway.   ;-)  Currently, this does the wrong thing,
> > and I think that maybe the prefix precedence should be fixed so that
> >
> > @$ary[0]
> >
> > is the same as
> >
> > @{$ary[0]}
> >
> > Or doe people find it intuitive that this returns a slice of an array
> > pointed to by $ary?
>
> I'd find
>
> my @a = @array[0;];
>
> to be nicer -- no references visible, unambiguous, same number of
> characters as @$array[0], and symmetrical with
>
> my @b = @array[;0];
>
> for a different dimension.  It isn't easy to do that with LOL notation.
>
I agree with both of you. It would be nice if @$ precedence worked as Bart
specified, but I still think that arrays should be arrays.





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