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

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

From:
Buddha Buck
Date:
September 25, 2000 15:51
Subject:
Re: RFC 204 (v2) Arrays: Use list reference for multidimensional array access
Message ID:
20000925225057.1614F15713@zaphod
> On Fri, 22 Sep 2000 08:58:10 +1100, Jeremy Howard wrote:
> 
> >Bart Lateur wrote:
> >> Hmm... the problem is, I think, that array references and ordinary
> >> scalars are both scalars.
> >>
> >That's true, but they're scalars with different interfaces. In particular,
> >an array ref can be dereferenced and provides an array in doing so. If an
> >index can do this, then it's a multidimensional index.
> 
> That worries me. I'd like to avoid to repeat the fiasco of having the
> same code have different kinds of behavious, depending on the contents
> of a variable, as we have with symbolic references versus anonymous
> arrays/hashes.
> 
> What will this return?
> 
> 	$ary[$index]
> 
> It looks like a plain and simple array item, but what if $index is an
> array ref?

Then it returns a plain and simple array item from a multidimensional 
array.

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.

If anyone is interested, I'll post my original .pod I wrote...


> 
> 
> >> What would be the difference between
> >>
> >> $a[2]
> >>
> >> and
> >>
> >> $a[[2]]
> >>
> >> anyway? Aren't these just the same?
> >>
> >Nearly! From the RFC:
> >
> ><quote>
> >When a listref is used to index a list of lists, the returned list reference
> >is automatically dereferenced:
> >
> >  my @array( [0,1], [1,2]);
> >  my @a = @array[[0]]; # Returns (0,1), _not_ [0,1]
> ></quote>

That's part of what I didn't write in "my" RFC.

I especially don't like the line

    $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?

That'll teach me to proofread what "I" say....

> 
> 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.

 
> You are right in that. You cannot get a hash slice of a Perl4 style
> "multidimensional" hash. A major stumbling block.

A fatal one, in my opinion.

> 
> -- 
> 	Bart.

-- 
     Buddha Buck                             bmbuck@14850.com
"Just as the strength of the Internet is chaos, so the strength of our
liberty depends upon the chaos and cacophony of the unfettered speech
the First Amendment protects."  -- A.L.A. v. U.S. Dept. of Justice





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