File Coverage

blib/lib/List/Group.pm
Criterion Covered Total %
statement 36 37 97.3
branch 8 12 66.6
condition 1 3 33.3
subroutine 9 9 100.0
pod 1 1 100.0
total 55 62 88.7


line stmt bran cond sub pod time code
1             package List::Group;
2             # $Id: Group.pm,v 1.3 2004/02/24 23:52:58 cwest Exp $
3 1     1   39065 use strict;
  1         2  
  1         34  
4 1     1   5 use base qw[Exporter];
  1         2  
  1         104  
5 1     1   5 use Carp;
  1         6  
  1         66  
6 1     1   885 use POSIX qw[ceil];
  1         7755  
  1         6  
7 1     1   2470 use Storable qw[dclone];
  1         5374  
  1         86  
8              
9 1     1   9 use vars qw[$VERSION @EXPORT_OK %EXPORT_TAGS];
  1         2  
  1         414  
10             $VERSION = (qw$Revision: 1.3 $)[1];
11             @EXPORT_OK = ( qw[group] );
12             %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 I, I
37              
38             my @table = group \@list, cols => 2;
39              
40             This function returns a list-of-lists containing the elements of I
41             passed as the first argument. The remaining arguments detail how to group the
42             elements. Available groupings are C, and C. Each of these groupings
43             accept a single digit as a value, the number of C or C 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             sub group($$$;) {
61 2     2 1 1256 my ($list, $group_by, $number) = @_;
62 2 50       12 croak "First argument to __PACKAGE__\::group() was not a list ref!"
63             unless ref($list) eq 'ARRAY';
64 2 50 33     12 croak "Number of $group_by passed to __PACKAGE__\::group() was not a digit"
65             if $number =~ /\D/ && grep $group_by, qw[cols rows];
66 2         3 my $grouping;
67 2 100       143 $grouping = _group_by_cols(dclone($list), $number) if $group_by eq 'cols';
68 2 100       97 $grouping = _group_by_rows(dclone($list), $number) if $group_by eq 'rows';
69 2 50       9 return wantarray ? @{$grouping} : $grouping if $grouping;
  2 50       14  
70 0         0 croak "Unrecognized group by argument '$group_by' to __PACKAGE__\::group()";
71             }
72              
73             =pod
74              
75             =back
76              
77             =cut
78              
79             sub _group_by_cols($$;) {
80 2     2   5 my ($list, $number) = @_;
81 2         5 my $grouping = [];
82 9         20 push @{$grouping}, [ splice @{$list}, 0, $number ]
  7         8  
  7         23  
83 2         3 while @{$list};
84 2         4 return $grouping;
85             }
86              
87             sub _group_by_rows($$;) {
88 1     1   3 my ($list, $number) = @_;
89 1         3 return _group_by_cols($list, ceil @{$list}/$number);
  1         22  
90             }
91              
92             1;
93              
94             __END__