File Coverage

blib/lib/Array/Transpose.pm
Criterion Covered Total %
statement 19 19 100.0
branch 6 8 75.0
condition 1 2 50.0
subroutine 4 4 100.0
pod 1 1 100.0
total 31 34 91.1


line stmt bran cond sub pod time code
1             package Array::Transpose;
2 3     3   347066 use strict;
  3         7  
  3         136  
3 3     3   28 use warnings;
  3         4  
  3         246  
4 3     3   26 use base qw{Exporter};
  3         8  
  3         1271  
5              
6             our $VERSION = '0.07';
7             our @EXPORT = qw(transpose);
8              
9             =head1 NAME
10              
11             Array::Transpose - Transposes a 2-Dimensional Array
12              
13             =head1 SYNOPSIS
14              
15             use Array::Transpose;
16             @array=transpose(\@array);
17              
18             use Array::Transpose qw{};
19             @array=Array::Transpose::transpose(\@array);
20              
21             Example:
22              
23             use Array::Transpose;
24             use Data::Dumper;
25             my $array=transpose([
26             [ 0 .. 4 ],
27             ["a" .. "e"],
28             ]);
29             print Data::Dumper->Dump([$array]);
30              
31             Returns
32            
33             $VAR1 = [
34             [ 0, 'a' ],
35             [ 1, 'b' ],
36             [ 2, 'c' ],
37             [ 3, 'd' ],
38             [ 4, 'e' ]
39             ];
40              
41             =head1 DESCRIPTION
42              
43             This package exports one function named transpose.
44              
45             In linear algebra, the transpose of a matrix A is another matrix A' created by any one of the following equivalent actions:
46              
47             =over 2
48              
49             =item *
50              
51             write the rows of A as the columns of A'
52              
53             =item *
54              
55             write the columns of A as the rows of A'
56              
57             =item *
58              
59             reflect A by its main diagonal (which starts from the top left) to obtain A'
60              
61             =back
62              
63             =head1 USAGE
64              
65             use Array::Transpose;
66             my @array = transpose(\@array);
67              
68             =head1 METHODS
69              
70             =head2 transpose
71              
72             Returns a transposed 2-Dimensional Array given a 2-Dimensional Array
73              
74             my $out = transpose($in); #$in=[[],[],[],...];
75             my @out = transpose(\@in); #@in=([],[],[],...);
76              
77             =cut
78              
79             sub transpose {
80 7 50   7 1 607961 die("Error: Expecting a single parameter")
81             unless @_ == 1;
82 7         19 my $in = shift; #[[],[],[]]
83 7 50       26 die("Error: Expecting parameter to be an array reference")
84             unless ref($in) eq "ARRAY";
85 7         19 my @out = ();
86 7 100       21 if (@$in > 0) {
87 5   50     10 my $cols = scalar(@{$in->[0]}) || 0;
88 5         20 foreach my $col (0 .. $cols-1) {
89 500016         759474 push @out, [map {$_->[$col]} @$in];
  500056         1178433  
90             }
91             }
92 7 100       45 return wantarray ? @out : \@out
93             }
94              
95             =head1 LIMITATIONS
96              
97             The transpose function assumes all rows have the same number of columns as the first row.
98              
99             =head1 AUTHOR
100              
101             Michael R. Davis
102              
103             =head1 COPYRIGHT
104              
105             This program is free software licensed under the...
106              
107             The BSD License
108              
109             The full text of the license can be found in the LICENSE file included with this module.
110              
111             =head1 SEE ALSO
112              
113             =head2 Similar Capabilities
114              
115             L method transpose, L rotate method
116              
117             =head2 Packages built on top of this package
118              
119             L
120              
121             =cut
122              
123             1;