| 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
| |||||||||||||||||||||||||||||
| 63 | unless ref($list) eq 'ARRAY'; | ||||||||||||||||||||||||||||
| 64 | 2 | ▶ | croak "Number of $group_by passed to __PACKAGE__\::group() was not a digit" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$number =~ /\D/ and grep $group_by, ("cols", "rows")
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")
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 68 | 2 | ▶ | $grouping = _group_by_rows(dclone($list), $number) if $group_by eq 'rows'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 2 | ▶ | return wantarray ? @{$grouping} : $grouping if $grouping; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||