File Coverage

blib/lib/Array/Util/MultiTarget.pm
Criterion Covered Total %
statement 75 75 100.0
branch 20 20 100.0
condition n/a
subroutine 10 10 100.0
pod 7 7 100.0
total 112 112 100.0


line stmt bran cond sub pod time code
1             ## no critic: Subroutines::ProhibitSubroutinePrototypes
2             package Array::Util::MultiTarget;
3              
4 2     2   404251 use strict;
  2         5  
  2         108  
5 2     2   17 use warnings;
  2         5  
  2         192  
6              
7 2     2   16 use Exporter qw(import);
  2         4  
  2         1333  
8              
9             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
10             our $DATE = '2023-12-03'; # DATE
11             our $DIST = 'Array-Util-MultiTarget'; # DIST
12             our $VERSION = '0.001'; # VERSION
13              
14             our @EXPORT_OK = qw(
15             mtpop
16             mtpush
17             mtsplice
18             mtremovestr
19             mtremoveallstr
20             mtremovenum
21             mtremoveallnum
22             );
23              
24             sub mtpop($) {
25 1     1 1 429482 my $arys = shift;
26              
27 1         2 my @res;
28 1         4 for my $ary (@$arys) { push @res, pop @$ary }
  2         8  
29 1         11 @res;
30             }
31              
32             sub mtpush($@) {
33 1     1 1 3809 my $arys = shift;
34              
35 1         3 for my $ary (@$arys) { push @$ary, @_ }
  2         11  
36             }
37              
38             sub mtsplice($$;$@) {
39 3     3 1 7504 my $arys = shift;
40 3         9 my $offset = shift;
41 3 100       4 my $len; $len = shift if @_;
  3         15  
42              
43 3         7 my @res;
44 3         8 for my $ary (@$arys) {
45 6 100       15 if (defined $len) {
46 4         17 push @res, [splice @$ary, $offset, $len, @_];
47             } else {
48 2         5 push @res, [splice @$ary, $offset];
49             }
50             }
51 3         10 @res;
52             }
53              
54             sub mtremovestr {
55 2     2 1 4522 my $arys = shift;
56 2         4 my $wanted = shift;
57              
58 2         2 my $pos;
59 2         4 for my $i (0 .. $#{ $arys->[0] }) {
  2         8  
60 5 100       12 if ($arys->[0][$i] eq $wanted) {
61 1         2 $pos = $i; last;
  1         2  
62             }
63             }
64 2 100       10 return unless defined $pos;
65              
66 1         3 for my $ary (@$arys) {
67 2         5 splice @$ary, $pos, 1;
68             }
69              
70 1         5 $pos;
71             }
72              
73             sub mtremoveallstr {
74 2     2 1 3639 my $arys = shift;
75 2         4 my $wanted = shift;
76              
77 2         26 my @pos;
78 2         4 for my $i (0 .. $#{ $arys->[0] }) {
  2         8  
79 8 100       18 if ($arys->[0][$i] eq $wanted) {
80 2         6 unshift @pos, $i;
81             }
82             }
83 2 100       8 return unless @pos;
84              
85 1         13 for my $ary (@$arys) {
86 2         5 for (@pos) {
87 4         9 splice @$ary, $_, 1;
88             }
89             }
90              
91 1         14 reverse @pos;
92             }
93              
94             sub mtremovenum {
95 2     2 1 4928 my $arys = shift;
96 2         4 my $wanted = shift;
97              
98 2         3 my $pos;
99 2         21 for my $i (0 .. $#{ $arys->[0] }) {
  2         8  
100 5 100       21 if ($arys->[0][$i] == $wanted) {
101 1         2 $pos = $i; last;
  1         2  
102             }
103             }
104 2 100       10 return unless defined $pos;
105              
106 1         2 for my $ary (@$arys) {
107 2         5 splice @$ary, $pos, 1;
108             }
109              
110 1         4 $pos;
111             }
112              
113             sub mtremoveallnum {
114 2     2 1 3505 my $arys = shift;
115 2         4 my $wanted = shift;
116              
117 2         3 my @pos;
118 2         21 for my $i (0 .. $#{ $arys->[0] }) {
  2         8  
119 8 100       20 if ($arys->[0][$i] == $wanted) {
120 2         5 unshift @pos, $i;
121             }
122             }
123 2 100       9 return unless @pos;
124              
125 1         3 for my $ary (@$arys) {
126 2         2 for (@pos) {
127 4         7 splice @$ary, $_, 1;
128             }
129             }
130              
131 1         4 reverse @pos;
132             }
133              
134             1;
135             # ABSTRACT: Apply an operation to multiple arrays
136              
137             __END__