blib/lib/List/Group.pm

filter: Statementstmt 97.5% 40 / 41 Branchbran 71.4% 10 / 14 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 87.1% 61 / 70 CC 9 SCAR 16.4
CC
9
Cov
88%
group
9.6
_group_by_cols
2.0
BEGIN
1.0
CRAP 5.2
↑ Summary
1 package List::Group;
2 # $Id: Group.pm,v 1.3 2004/02/24 23:52:58 cwest Exp $
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use base qw[Exporter];
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use POSIX qw[ceil];
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Storable qw[dclone];
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use vars qw[$VERSION @EXPORT_OK %EXPORT_TAGS];
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 $VERSION = (qw$Revision: 1.3 $)[1];
11 1 @EXPORT_OK = ( qw[group] );
12 1 %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
13  
14 =head1 NAME
15  
16 List::Group - Group a list of data structures to your specifications.
17  
18 =head1 SYNOPSIS
19  
20 use List::Group qw[group];
21 my @list = qw[cat dog cow rat];
22 my @group = group @list, cols => 2;
23  
24 foreach my $row ( @group ) {
25 print "@{$row}\n";
26 }
27  
28 =head1 DESCRIPTION
29  
30 A simple module that currently allows you to group a list by columns or rows.
31  
32 =head2 Functions
33  
34 =over 8
35  
36 =item C<group> I<listref>, I<args>
37  
38 my @table = group \@list, cols => 2;
39  
40 This function returns a list-of-lists containing the elements of I<listref>
41 passed as the first argument. The remaining arguments detail how to group the
42 elements. Available groupings are C<cols>, and C<rows>. Each of these groupings
43 accept a single digit as a value, the number of C<cols> or C<rows> to create.
44  
45 The following is what C<@table> would look like from the previous example.
46  
47 my @list = qw[cat dog mouse rat];
48 my @table = group \@list, cols => 2;
49  
50 print Dumper \@table;
51 __END__
52  
53 $VAR1 = [
54 [ 'cat', 'dog' ],
55 [ 'mouse', 'rat' ]
56 ];
57  
58 =cut
59  
60 2 ▶sub group($$$;) {
Subroutine group: called CC=8 SCAR=22.6
Pod documented
61 2 my ($list, $group_by, $number) = @_;
62 2 ▶ croak "First argument to __PACKAGE__\::group() was not a list ref!"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $list eq "ARRAY" 0 2 2
63 unless ref($list) eq 'ARRAY';
64 2 ▶ croak "Number of $group_by passed to __PACKAGE__\::group() was not a digit"
Branch: 1 branch
BranchTrueFalseTotal
if $number =~ /\D/ and grep $group_by, ("cols", "rows") 0 2 2
Condition33%1/3
$number =~ /\D/ and grep $group_by, ("cols", "rows")
!ll&&!rl&&r
200
MC/DC0%0/2
$number =~ /\D/ and grep $group_by, ("cols", "rows")
$number =~ /\D/ grep $group_by, ("cols", "rows")
Truth table33%1/3
A and B
A= $number =~ /\D/B= grep $group_by, ("cols", "rows")
ABresult
0X0
100
111
65 if $number =~ /\D/ && grep $group_by, qw[cols rows];
66 2 my $grouping;
67 2 ▶ $grouping = _group_by_cols(dclone($list), $number) if $group_by eq 'cols';
Branch: 1 branch
BranchTrueFalseTotal
if $group_by eq "cols" 1 1 2
68 2 ▶ $grouping = _group_by_rows(dclone($list), $number) if $group_by eq 'rows';
Branch: 1 branch
BranchTrueFalseTotal
if $group_by eq "rows" 1 1 2
69 2 ▶ return wantarray ? @{$grouping} : $grouping if $grouping;
Branch: 2 branches
BranchTrueFalseTotal
if $grouping 2 0 2
wantarray ? : 2 0 2
70 0 croak "Unrecognized group by argument '$group_by' to __PACKAGE__\::group()";
71 }
72  
73 =pod
74  
75 =back
76  
77 =cut
78  
79 2 ▶sub _group_by_cols($$;) {
Subroutine _group_by_cols: called CC=2 SCAR=6.9
80 2 my ($list, $number) = @_;
81 2 my $grouping = [];
82 7 push @{$grouping}, [ splice @{$list}, 0, $number ]
83 2 ▶ while @{$list};
Branch: 1 branch
BranchTrueFalseTotal
@{$list;} and do { push @{$grouping;}, [splice(@{$list;}, 0, $number)] } 7 2 9
84 2 return $grouping;
85 }
86  
87 1 ▶sub _group_by_rows($$;) {
Subroutine _group_by_rows: called CC=1 SCAR=0.0
88 1 my ($list, $number) = @_;
89 1 return _group_by_cols($list, ceil @{$list}/$number);
90 }
91  
92 1 1;
93  
94 __END__
↑ Summary