File Coverage

blib/lib/Array/Transpose/Ragged.pm
Criterion Covered Total %
statement 35 35 100.0
branch 3 4 75.0
condition n/a
subroutine 8 8 100.0
pod 1 1 100.0
total 47 48 97.9


line stmt bran cond sub pod time code
1             package Array::Transpose::Ragged;
2              
3 1     1   27692 use warnings;
  1         2  
  1         41  
4 1     1   7 use strict;
  1         2  
  1         43  
5 1     1   1060 use Array::Transpose;
  1         438  
  1         68  
6 1     1   7 use Carp;
  1         2  
  1         86  
7 1     1   5 use Exporter;
  1         1  
  1         367  
8             our @ISA = qw(Exporter);
9             our @EXPORT_OK = qw(transpose_ragged);
10              
11             =head1 NAME
12              
13             Array::Transpose::Ragged - Transpose a ragged array
14              
15             =head1 VERSION
16              
17             Version 0.01
18              
19             =cut
20              
21             our $VERSION = '0.01';
22              
23              
24             =head1 SYNOPSIS
25              
26             Array::Transpose is a handy module to transpose a regular matrix. However,
27             using it with an irregular matrix will result in data loss. This module
28             transposes a ragged matrix. Note that it will not preserve values which are
29             C.
30              
31              
32             use warnings; use strict;
33             use Array::Transpose::Ragged qw/transpose_ragged/;
34              
35             my @array = (
36             [qw /00 01/],
37             [qw /10 11 12/],
38             [qw /20 21/],
39             [qw /30 31 32 33 34/],
40             );
41              
42             my @transpose = transpose_ragged(\@array);
43              
44             The variable @transpose will now be:
45              
46             @transpose = (['00' ,'10' ,'20' ,'30'],
47             ['01' ,'11' ,'21' ,'31'],
48             [undef,'12' ,undef,'32'],
49             [undef,undef,undef,'33'],
50             [undef,undef,undef,'34']
51             );
52              
53             =head1 EXPORT
54              
55             C
56              
57             =head1 SUBROUTINES/METHODS
58              
59             =head2 transpose_ragged(\@array)
60              
61             =cut
62              
63             sub transpose_ragged {
64 1     1 1 237 my ($array) = @_;
65 1 50       7 croak "transpose_ragged only accepts an array reference" if ref($array) ne 'ARRAY';
66 1         4 my $normalised = _normalise_lengths($array);
67 1         6 my @t = transpose($normalised);
68             return @t
69 1         356 }
70              
71             =head2 _max_idx ($matrix)
72              
73             returns the max index length of the matrix
74              
75             =cut
76              
77             sub _max_idx {
78 1     1   1 my ($matrix) = @_;
79 1         2 my $max = 0;
80 1         3 foreach my $a (@$matrix) {
81 4         4 my $length = $#{$a};
  4         5  
82 4 100       11 $max = $length if $length > $max;
83             }
84 1         3 return $max;
85             }
86              
87             =head2 _normalise_lengths
88              
89             normalises the length of the matrix prior to calling C
90              
91             =cut
92              
93             sub _normalise_lengths {
94 1     1   2 my ($matrix) = @_;
95 1         4 my $max = _max_idx($matrix);
96 1         2 foreach my $m (@$matrix) {
97 4         3 my $length = $#{$m};
  4         5  
98 4         10 for (($length + 1) .. $max) {
99 8         13 $m->[$_] = undef;
100             }
101             }
102 1         3 return $matrix;
103             }
104              
105              
106             =head1 AUTHOR
107              
108             Kieren Diment, C<< >>
109              
110             =head1 BUGS
111              
112             The implementation could probably be far more efficient.
113              
114             Please report any bugs or feature requests to C
115             rt.cpan.org>, or through the web interface at
116             L. I
117             will be notified, and then you'll automatically be notified of progress on your
118             bug as I make changes.
119              
120             =head1 SUPPORT
121              
122             You can find documentation for this module with the perldoc command.
123              
124             perldoc Array::Transpose::Ragged
125              
126              
127             You can also look for information at:
128              
129             =over 4
130              
131             =item * RT: CPAN's request tracker
132              
133             L
134              
135             =item * AnnoCPAN: Annotated CPAN documentation
136              
137             L
138              
139             =item * CPAN Ratings
140              
141             L
142              
143             =item * Search CPAN
144              
145             L
146              
147             =back
148              
149              
150             =head1 ACKNOWLEDGEMENTS
151              
152              
153             =head1 LICENSE AND COPYRIGHT
154              
155             Copyright 2010 Kieren Diment.
156              
157             This program is released under the following license: BSD
158              
159             See http://dev.perl.org/licenses/ for more information.
160              
161              
162             =cut
163              
164             1; # End of Array::Transpose::Ragged