[% setvar title Arrays: @#arr for getting the dimensions of an array %]

This file is part of the Perl 6 Archive

Note: these documents may be out of date. Do not use as reference!

To see what is currently happening visit http://www.perl6.org/

TITLE

Arrays: @#arr for getting the dimensions of an array

VERSION

  Maintainer: Buddha Buck <bmbuck@14850.com>
  Date: 8 Sep 2000
  Last Modified: 19 Sep 2000
  Mailing List: perl6-language-data@perl.org
  Number: 206
  Version: 2
  Status: Frozen

ABSTRACT

This RFC proposes the use of the @# prefix to get the bounds and dimensionality of a multidimensional array.

DESCRIPTION

The $#array notation is useful for getting the upper bound of a unidimensional array, but is not as useful for getting the upper bounds of a multidimensional array. A multidimensional array should have a list of upper bounds, not a single upper bound.

This RFC proposes using @#array, analogous to $#array, to get the list of upper bounds for a multidimensional array @array. The length of @#array would indicate the dimensionality of @array.

Example

   sub printbounds (\@) {
     my $arrayref = shift;
     my $array = @$arrayref;
     print "D:", scalar @#arrayref," B:(",join(',',@#arrayref),")\n";
   }

   my int @a :bounds(2,2,2,2,2);
   printbounds(@a); # "D:5 B:(2,2,2,2,2)\n"

   my int @b :bounds(@a); # create @b the same size as @a
   printbounds(@b); # "D:5 B:(2,2,2,2,2)\n"

   my @c = (1,2,3,4,5,6,7,8,9,10);
   printbounds(@c); # "D:1 B:(9)\n"

IMPLEMENTATION

Presumably, multidimensional arrays would have to store their bounds somewhere to have the semantics provided in RFC 203. This operator would simply return to the user those stored bounds.

Where an array is declared without ':bounds', @# returns the largest bounds of each dimension that has been accessed:

   my int @d = ();
   $d[[3,4]] = 5;
   @e = @#d;        # (3,4)

REFERENCES

RFC 203: Notation for declaring and creating arrays