blib/lib/Array/Utils.pm

filter: Statementstmt 100% 31 / 31 Branchbran 100% 2 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod 100% 4 / 4 totaltotal 100% 42 / 42 CC 2 SCAR 2.9
CC
2
Cov
100%
array_diff
2.0
BEGIN
1.0
unique
1.0
CRAP 1.3
↑ Summary
1 package Array::Utils;
2  
3 =head1 NAME
4  
5 Array::Utils - small utils for array manipulation
6  
7 =head1 SYNOPSIS
8  
9 use Array::Utils qw(:all);
10
11 my @a = qw( a b c d );
12 my @b = qw( c d e f );
13  
14 # symmetric difference
15 my @diff = array_diff(@a, @b);
16  
17 # intersection
18 my @isect = intersect(@a, @b);
19
20 # unique union
21 my @unique = unique(@a, @b);
22
23 # check if arrays contain same members
24 if ( !array_diff(@a, @b) ) {
25 # do something
26 }
27
28 # get items from array @a that are not in array @b
29 my @minus = array_minus( @a, @b );
30
31 =head1 DESCRIPTION
32  
33 A small pure-perl module containing list manipulation routines. The module
34 emerged because I was tired to include same utility routines in numerous projects.
35  
36 =head1 FUNCTIONS
37  
38 =over 4
39  
40 =item C<unique>
41  
42 Returns an array of unique items in the arguments list.
43  
44 =item C<intersect>
45  
46 Returns an intersection of two arrays passed as arguments, keeping the order of the
47 second parameter. A nice side effect of this function can be exploited in situations as:
48  
49 @atreides = qw( Leto Paul Alia 'Leto II' );
50 @mylist = qw( Alia Leto );
51 @mylist = intersect( @mylist, @atreides ); # and @mylist is ordered as Leto,Alia
52  
53 =item C<array_diff>
54  
55 Return symmetric difference of two arrays passed as arguments.
56  
57 =item C<array_minus>
58  
59 Returns the difference of the passed arrays A and B (only those
60 array elements that exist in A and do not exist in B).
61 If an empty array is returned, A is subset of B.
62  
63 Function was proposed by Laszlo Forro <salmonix@gmail.com>.
64  
65 =back
66  
67 =head1 BUGS
68  
69 None known yet
70  
71 =head1 AUTHOR
72  
73 Sergei A. Fedorov <zmij@cpan.org>
74  
75 I will be happy to have your feedback about the module.
76  
77 =head1 COPYRIGHT
78  
79 This module is Copyright (c) 2007 Sergei A. Fedorov.
80 All rights reserved.
81  
82 You may distribute under the terms of either the GNU General Public
83 License or the Artistic License, as specified in the Perl README file.
84  
85 =head1 WARRANTY
86  
87 This is free software. IT COMES WITHOUT WARRANTY OF ANY KIND.
88  
89 =cut
90  
91 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
92  
93 1 require Exporter;
94 1 our @ISA = qw(Exporter);
95  
96 1 our %EXPORT_TAGS = (
97 all => [ qw(
98 &unique
99 &intersect
100 &array_diff
101 &array_minus
102 ) ],
103 );
104 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
105  
106 1 our $VERSION = '0.5';
107  
108 1 ▶sub unique(@) {
Subroutine unique: called CC=1 SCAR=0.0
Pod documented
109 1 return keys %{ {map { $_ => undef } @_}};
110 }
111  
112 1 ▶sub intersect(\@\@) {
Subroutine intersect: called CC=1 SCAR=0.0
Pod documented
113 1 my %e = map { $_ => undef } @{$_[0]};
114 1 return grep { exists( $e{$_} ) } @{$_[1]};
115 }
116  
117 11 ▶sub array_diff(\@\@) {
Subroutine array_diff: called CC=2 SCAR=6.9
Pod documented
118 11 my %e = map { $_ => undef } @{$_[1]};
119 11 ▶ return @{[ ( grep { (exists $e{$_}) ? ( delete $e{$_} ) : ( 1 ) } @{ $_[0] } ), keys %e ] };
Branch: 1 branch
BranchTrueFalseTotal
exists $e{$_} ? : 26 8 34
120 }
121  
122 3 ▶sub array_minus(\@\@) {
Subroutine array_minus: called CC=1 SCAR=0.0
Pod documented
123 3 my %e = map{ $_ => undef } @{$_[1]};
124 3 return grep( ! exists( $e{$_} ), @{$_[0]} );
125 }
126  
127 1 1;
↑ Summary