develooper Front page | | Postings from September 2000

RFC 272 (v1) Arrays: transpose()

Perl6 RFC Librarian
September 21, 2000 19:25
RFC 272 (v1) Arrays: transpose()
Message ID:
This and other RFCs are available on the web at

=head1 TITLE

Arrays: transpose()

=head1 VERSION

  Maintainer: Jeremy Howard <>
  Date: 22 Sep 2000
  Mailing List:
  Number: 272
  Version: 1
  Status: Developing


It is proposed that a new function C<transpose> be added to Perl.
C<transpose($dim1, $dim2, @list)> would return @list with $dim1 and $dim2
switched. It would return an alias into the original list, not a copy of
the elements.


It is proposed that Perl implement a function called C<transpose> that
transposes two dimensions of an array, and is evaluated lazily. L<RFC 202>
gives an overview of the proposed multidimensional arrays that
C<transpose> works with. For instance:

  @a = ([1,2],[3,4],[5,6]);
  @transposed_list = transpose(0,1,@a);   # ([1,3,5],[2,4,6])

This is different to C<reshape> (see L<RFC 148>) which does not reorder
its elements:

  @a = ([1,2],[3,4],[5,6]);
  @reshaped_list = reshape([3,2],@a);   # ([1,2,3],[4,5,6])

C<transpose> is its own inverse:

  @transposed_list = transpose(0,1,@a);   # ([1,3,5],[2,4,6])
  @orig_list = transpose(0,1,@transposed_list); # (([1,2],[3,4],[5,6])
  @a == @orig_list;   # true

If C<transpose> refers to a dimension that does not exist, empty
dimensions autovivify as necessary:

  @row_vector = (1,2,3,4);
  @col_vector = transpose(0,1,@row_vector);   # ([1],[2],[3],[4])

C<transpose> does not make a copy of the elements of its arguments; it
simply create an alias:

  @row_vector = (1,2,3,4);
  @col_vector = transpose(0,1,@row_vector);  # ([1],[2],[3],[4])
  $col_vector[[0,1]] = 0;
  @row_vector == (1,0,3,4);                  # True

RFC 90 discusses possible approaches to implementing aliasing.


RFC 90: Arrays: merge() and unmerge()

RFC 148: Arrays: Add reshape() for multi-dimensional array reshaping Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About