blib/lib/Array/Transpose/Ragged.pm

filter: Statementstmt 100% 39 / 39 Branchbran 75.0% 3 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 98.0% 51 / 52 CC 6 SCAR 7.3
CC
6
Cov
98%
_max_idx
3.0
_normalise_lengths
3.0
transpose_ragged
2.0
CRAP 2.1
↑ Summary
1 package Array::Transpose::Ragged;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Array::Transpose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 our @ISA = qw(Exporter);
9 2 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 2 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<undef>.
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<transpose_ragged(\@array)>
56  
57 =head1 SUBROUTINES/METHODS
58  
59 =head2 transpose_ragged(\@array)
60  
61 =cut
62  
63 1 ▶sub transpose_ragged {
Subroutine transpose_ragged: called CC=2 SCAR=7.0
Pod documented
64 1 my ($array) = @_;
65 1 ▶ croak "transpose_ragged only accepts an array reference" if ref($array) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $array ne "ARRAY" 0 1 1
66 1 my $normalised = _normalise_lengths($array);
67 1 my @t = transpose($normalised);
68 return @t
69 1 }
70  
71 =head2 _max_idx ($matrix)
72  
73 returns the max index length of the matrix
74  
75 =cut
76  
77 1 ▶sub _max_idx {
Subroutine _max_idx: called CC=3 SCAR=11.0
78 1 my ($matrix) = @_;
79 1 my $max = 0;
80 1 foreach my $a (@$matrix) {
81 4 my $length = $#{$a};
82 4 ▶ $max = $length if $length > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $length > $max 3 1 4
83 }
84 1 return $max;
85 }
86  
87 =head2 _normalise_lengths
88  
89 normalises the length of the matrix prior to calling C<transpose>
90  
91 =cut
92  
93 1 ▶sub _normalise_lengths {
Subroutine _normalise_lengths: called CC=3 SCAR=11.0
94 1 my ($matrix) = @_;
95 1 my $max = _max_idx($matrix);
96 1 foreach my $m (@$matrix) {
97 4 my $length = $#{$m};
98 4 for (($length + 1) .. $max) {
99 8 $m->[$_] = undef;
100 }
101 }
102 1 return $matrix;
103 }
104  
105  
106 =head1 AUTHOR
107  
108 Kieren Diment, C<< <zarquon at cpan.org> >>
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<bug-array-transpose-ragged at
115 rt.cpan.org>, or through the web interface at
116 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Array-Transpose-Ragged>. 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<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Array-Transpose-Ragged>
134  
135 =item * AnnoCPAN: Annotated CPAN documentation
136  
137 L<http://annocpan.org/dist/Array-Transpose-Ragged>
138  
139 =item * CPAN Ratings
140  
141 L<http://cpanratings.perl.org/d/Array-Transpose-Ragged>
142  
143 =item * Search CPAN
144  
145 L<http://search.cpan.org/dist/Array-Transpose-Ragged/>
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 2 1; # End of Array::Transpose::Ragged
↑ Summary