line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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 |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
Returns an array of unique items in the arguments list. |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=item C |
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 |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
Return symmetric difference of two arrays passed as arguments. |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=item C |
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 . |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=back |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=head1 BUGS |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
None known yet |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=head1 AUTHOR |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
Sergei A. Fedorov |
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
|
|
|
1
|
|
23675
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
400
|
|
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
require Exporter; |
94
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( |
97
|
|
|
|
|
|
|
all => [ qw( |
98
|
|
|
|
|
|
|
&unique |
99
|
|
|
|
|
|
|
&intersect |
100
|
|
|
|
|
|
|
&array_diff |
101
|
|
|
|
|
|
|
&array_minus |
102
|
|
|
|
|
|
|
) ], |
103
|
|
|
|
|
|
|
); |
104
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
our $VERSION = '0.5'; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub unique(@) { |
109
|
1
|
|
|
1
|
1
|
3
|
return keys %{ {map { $_ => undef } @_}}; |
|
1
|
|
|
|
|
3
|
|
|
8
|
|
|
|
|
31
|
|
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
sub intersect(\@\@) { |
113
|
1
|
|
|
1
|
1
|
4
|
my %e = map { $_ => undef } @{$_[0]}; |
|
4
|
|
|
|
|
12
|
|
|
1
|
|
|
|
|
3
|
|
114
|
1
|
|
|
|
|
4
|
return grep { exists( $e{$_} ) } @{$_[1]}; |
|
4
|
|
|
|
|
12
|
|
|
1
|
|
|
|
|
3
|
|
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub array_diff(\@\@) { |
118
|
11
|
|
|
11
|
1
|
2488
|
my %e = map { $_ => undef } @{$_[1]}; |
|
34
|
|
|
|
|
98
|
|
|
11
|
|
|
|
|
31
|
|
119
|
11
|
100
|
|
|
|
21
|
return @{[ ( grep { (exists $e{$_}) ? ( delete $e{$_} ) : ( 1 ) } @{ $_[0] } ), keys %e ] }; |
|
11
|
|
|
|
|
13
|
|
|
34
|
|
|
|
|
143
|
|
|
11
|
|
|
|
|
37
|
|
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub array_minus(\@\@) { |
123
|
3
|
|
|
3
|
1
|
6
|
my %e = map{ $_ => undef } @{$_[1]}; |
|
8
|
|
|
|
|
24
|
|
|
3
|
|
|
|
|
10
|
|
124
|
3
|
|
|
|
|
8
|
return grep( ! exists( $e{$_} ), @{$_[0]} ); |
|
3
|
|
|
|
|
21
|
|
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
1; |