blib/lib/Tie/FieldVals/Select.pm

filter: Statementstmt 46.1% 136 / 295 Branchbran 33.5% 45 / 134 Conditioncond 35.8% 24 / 67 MC/DCmcdc 17.5% 7 / 40 Subroutinesub 64.0% 16 / 25 Podpod 100% 7 / 7 totaltotal 41.3% 235 / 568 CC 95 SCAR 51.5
CC
95
Cov
41%
set_sel_slice
702.0
make_selection
46.4
sort_records
35.2
CRAP 172.3
↑ Summary blib/lib/Tie/FieldVals/Row.pm »
1 package Tie::FieldVals::Select;
2 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =head1 NAME
6  
7 Tie::FieldVals::Select - an array tie for a subset of Tie::FieldVals data
8  
9 =head1 VERSION
10  
11 This describes version B<0.6203> of Tie::FieldVals::Select.
12  
13 =cut
14  
15 8 our $VERSION = '0.6203';
16  
17 =head1 SYNOPSIS
18  
19 use Tie::FieldVals;
20 use Tie::FieldVals::Row;
21 use Tie::FieldVals::Select;
22  
23 my @sel_recs;
24 my $sel_obj = tie @sel_recs, 'Tie::FieldVals::Select',
25 datafile=>$datafile, selection=>{$key=>$value...};
26  
27 # sort the records
28 $sel_obj->sort_records(sort_by=>@sort_order);
29  
30 =head1 DESCRIPTION
31  
32 This is a Tie object to map a SUBSET of the records in a Tie::FieldVals
33 data file into an array. It is a sub-class of Tie::FieldVals. This is
34 useful as a separate object because one can do things to it without
35 affecting the underlying file, unlike with a Tie::FieldVals object. One
36 can re-select the data, sort the data, or take a temporary "slice" of the
37 data.
38  
39 This depends on the Tie::FieldVals and Tie::FieldVals::Row modules.
40  
41 =cut
42  
43 8 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 8 ▶use Tie::FieldVals;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 8 ▶use Tie::FieldVals::Row;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 8 ▶use Fcntl qw(:DEFAULT);
Subroutine BEGIN: called CC=1 SCAR=0.0
49 8 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 8 our @ISA = qw(Tie::FieldVals);
52  
53 # to make taint happy
54 8 $ENV{PATH} = "/bin:/usr/bin:/usr/local/bin";
55 8 $ENV{CDPATH} = '';
56 8 $ENV{BASH_ENV} = '';
57  
58 # for debugging
59 8 my $DEBUG = 0;
60  
61 #================================================================
62 # Methods
63  
64 =head1 OBJECT METHODS
65  
66 =head2 make_selection
67  
68 Select the records (again). Resets the selection and re-makes it
69 with the new selection criteria.
70  
71 $arr_obj->make_selection(selection=>{$key=>$value},
72 match_any=>$val2);
73  
74 $arr_obj->make_selection(selection=>$value);
75  
76 =cut
77 8 ▶sub make_selection {
Subroutine make_selection: called CC=17 SCAR=38.4
Pod documented
78 8 my $self = shift;
79 8 my %args = (
80 selection=>undef,
81 match_any=>undef,
82 @_
83 );
84 8 my $select = $args{selection};
85 8 my $match_any = $args{match_any};
86  
87 # now, apply the selection to the records
88 8 my @records = ();
89 8 my $count = $self->SUPER::FETCHSIZE();
90 8 ▶ for (my $i=0; $i < $count; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $count) 697 8 705
91 {
92 697 my $add_this_row = 0;
93 # if there is no 'selection' then get all the records
94 697 ▶ if ((!defined $select || !$select)
Branch: 5 branches
BranchTrueFalseTotal
if (not defined $select && $select || defined $match_any && $match_any) { } 196 501 697
elsif (not ref $select) { } 0 501 501
elsif (defined $match_any and $match_any) { } 0 501 501
elsif (ref $select eq 'ARRAY') { } 0 501 501
elsif (ref $select eq 'HASH') { } 501 0 501
Condition55%5/9
defined $select && $select || defined $match_any && $match_any
l!l&&r!l&&!r
5010196
defined $select and $select
!ll&&!rl&&r
1960501
defined $match_any and $match_any
!ll&&!rl&&r
19600
MC/DC25%1/4
defined $select && $select || defined $match_any && $match_any
defined $select $select defined $match_any $match_any
Truth table28%2/7
A and B || C and D
A= defined $selectB= $selectC= defined $match_anyD= $match_any
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
95 && (!defined $match_any || !$match_any))
96 {
97 196 $add_this_row = 1;
98 }
99 elsif (!ref $select) # match any
100 {
101 0 my $row_ref = $self->SUPER::FETCH($i);
102 0 my $row_obj = tied %{$row_ref};
103 0 ▶ if ($row_obj->match_any($select))
Branch: 1 branch
BranchTrueFalseTotal
if ($row_obj->match_any($select)) 0 0 0
104 {
105 0 $add_this_row = 1;
106 }
107 }
108 ▶ elsif (defined $match_any && $match_any)
Condition33%1/3
defined $match_any and $match_any
!ll&&!rl&&r
50100
MC/DC0%0/2
defined $match_any and $match_any
defined $match_any $match_any
Truth table33%1/3
A and B
A= defined $match_anyB= $match_any
ABresult
0X0
100
111
109 {
110 0 my $row_ref = $self->SUPER::FETCH($i);
111 0 my $row_obj = tied %{$row_ref};
112 0 ▶ if ($row_obj->match_any($match_any))
Branch: 1 branch
BranchTrueFalseTotal
if ($row_obj->match_any($match_any)) 0 0 0
113 {
114 0 $add_this_row = 1;
115 }
116 }
117 elsif (ref $select eq 'ARRAY') # select a range
118 {
119 0 my $first = ${$select}[0];
120 0 my $last = ${$select}[1];
121 0 ▶ if ($i >= $first && $i <= $last)
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= $first and $i <= $last) 0 0 0
Condition0%0/3
$i >= $first and $i <= $last
!ll&&!rl&&r
000
MC/DC0%0/2
$i >= $first and $i <= $last
$i >= $first $i <= $last
Truth table0%0/3
A and B
A= $i >= $firstB= $i <= $last
ABresult
0X0
100
111
122 {
123 0 $add_this_row = 1;
124 }
125 }
126 elsif (ref $select eq 'HASH')
127 {
128 501 my $row_ref = $self->SUPER::FETCH($i);
129 501 my $row_obj = tied %{$row_ref};
130 501 ▶ if ($row_obj->match(%{$select}))
Branch: 1 branch
BranchTrueFalseTotal
if ($row_obj->match(%{$select;})) 164 337 501
131 {
132 164 $add_this_row = 1;
133 }
134 }
135 # add the index for this row to our records
136 697 ▶ if ($add_this_row)
Branch: 1 branch
BranchTrueFalseTotal
if ($add_this_row) 360 337 697
137 {
138 360 push @records, $i;
139 }
140 }
141 8 $self->{SEL_RECS} = \@records;
142 # set the full slice
143 8 $self->{OPTIONS}->{start_rec} = 0;
144 8 $self->{OPTIONS}->{num_recs} = scalar @{$self->{SEL_RECS}};
145  
146 } # make_selection
147  
148 =head2 set_sel_slice
149  
150 Set this selection to a sub-set of itself. In other words,
151 keep the original selection, but perform all operations
152 on a slice of it. Assumes the array is sorted, and that
153 the selection is related to the sort order (for example,
154 that I<key1>=>I<value1> where I<key1> is the first key of the sort
155 order).
156  
157 $arr_obj->set_sel_slice(selection=>{$key=>$value},
158 match_any=>$val2,
159 start_at_zero=>0);
160  
161 =cut
162 0 ▶sub set_sel_slice {
Subroutine set_sel_slice: not called CC=26 SCAR=65.5
Pod documented
163 0 my $self = shift;
164 0 my %args = (
165 selection=>undef,
166 match_any=>undef,
167 start_at_zero=>0,
168 @_
169 );
170  
171 0 my $select = $args{selection};
172 0 my $match_any = $args{match_any};
173 0 ▶ if ($DEBUG)
Branch: 1 branch
BranchTrueFalseTotal
if ($DEBUG) 0 0 0
174 {
175 0 print STDERR "set_sel_slice:";
176 0 print STDERR " selection=";
177 0 print STDERR Dumper($select);
178 }
179  
180 # now, apply the sub-selection to the current selection
181 0 my @records = ();
182 0 my $count = @{$self->{SEL_RECS}};
183 0 my $start_range = 0;
184 0 my $start_offset;
185 0 ▶ if ($args{start_at_zero})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'start_at_zero'}) { } 0 0 0
186 {
187 0 $self->{OPTIONS}->{start_rec} = 0;
188 0 $start_offset = 0;
189 }
190 else
191 {
192 # start from the next record after the current slice
193 # but only if we have been slicing
194 0 ▶ if ($self->{OPTIONS}->{num_recs} < $count)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"OPTIONS"}{"num_recs"} < $count) 0 0 0
195 {
196 $self->{OPTIONS}->{start_rec} +=
197 0 $self->{OPTIONS}->{num_recs};
198 }
199 0 $start_offset = $self->{OPTIONS}->{start_rec};
200 # set the count to be from the offset start
201 # to the end of the SEL_RECS array
202 0 $count = ($count - $self->{OPTIONS}->{start_rec});
203 }
204 # reset the curent slice to be as big as possible
205 0 $self->{OPTIONS}->{num_recs} = $count;
206  
207 0 my $end_range = 0;
208 0 my $matches = 0;
209 0 my $this_row_matches = 0;
210 0 my $match_found = 0;
211 0 ▶ if ($DEBUG)
Branch: 1 branch
BranchTrueFalseTotal
if ($DEBUG) 0 0 0
212 {
213 0 print STDERR "set_sel_slice: checking $count records\n";
214 }
215 0 ▶ for (my $i=0; $i < $count; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $count) 0 0 0
216 {
217 0 $this_row_matches = 0;
218 # if there is no 'selection' then get all the records
219 0 ▶ if (!defined $select && !defined $match_any)
Branch: 5 branches
BranchTrueFalseTotal
if (not defined $select || defined $match_any) { } 0 0 0
elsif (not ref $select) { } 0 0 0
elsif (ref $select eq 'ARRAY') { } 0 0 0
elsif (ref $select eq 'HASH') { } 0 0 0
elsif (defined $match_any and $match_any) { } 0 0 0
Condition0%0/3
defined $select || defined $match_any
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $select || defined $match_any
defined $select defined $match_any
Truth table0%0/3
A || B
A= defined $selectB= defined $match_any
ABresult
1X1
011
000
220 {
221 0 $this_row_matches = 1;
222 }
223 elsif (!ref $select) # match any
224 {
225 0 my $row_ref = $self->FETCH($i);
226 0 my $row_obj = tied %{$row_ref};
227 0 ▶ if ($row_obj->match_any($select))
Branch: 1 branch
BranchTrueFalseTotal
if ($row_obj->match_any($select)) 0 0 0
228 {
229 0 $this_row_matches = 1;
230 }
231 }
232 elsif (ref $select eq 'ARRAY')
233 {
234 0 my $first = ${$select}[0];
235 0 my $last = ${$select}[1];
236 0 ▶ if ($i >= $first && $i <= $last)
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= $first and $i <= $last) 0 0 0
Condition0%0/3
$i >= $first and $i <= $last
!ll&&!rl&&r
000
MC/DC0%0/2
$i >= $first and $i <= $last
$i >= $first $i <= $last
Truth table0%0/3
A and B
A= $i >= $firstB= $i <= $last
ABresult
0X0
100
111
237 {
238 0 $this_row_matches = 1;
239 }
240 }
241 elsif (ref $select eq 'HASH')
242 {
243 0 my $row_ref = $self->FETCH($i);
244 0 my $row_obj = tied %{$row_ref};
245 0 ▶ if ($row_obj->match(%{$select}))
Branch: 1 branch
BranchTrueFalseTotal
if ($row_obj->match(%{$select;})) 0 0 0
246 {
247 0 $this_row_matches = 1;
248 0 ▶ if ($DEBUG)
Branch: 1 branch
BranchTrueFalseTotal
if ($DEBUG) 0 0 0
249 {
250 0 print STDERR "row=[$i]";
251 0 print STDERR Dumper($row_ref);
252 }
253 }
254 }
255 ▶ elsif (defined $match_any && $match_any)
Condition0%0/3
defined $match_any and $match_any
!ll&&!rl&&r
000
MC/DC0%0/2
defined $match_any and $match_any
defined $match_any $match_any
Truth table0%0/3
A and B
A= defined $match_anyB= $match_any
ABresult
0X0
100
111
256 {
257 0 my $row_ref = $self->FETCH($i);
258 0 my $row_obj = tied %{$row_ref};
259 0 ▶ if ($row_obj->match_any($match_any))
Branch: 1 branch
BranchTrueFalseTotal
if ($row_obj->match_any($match_any)) 0 0 0
260 {
261 0 $this_row_matches = 1;
262 }
263 }
264 # have we started matching?
265 0 ▶ if (!$matches)
Branch: 1 branch
BranchTrueFalseTotal
unless ($matches) 0 0 0
266 {
267 0 ▶ if ($this_row_matches)
Branch: 1 branch
BranchTrueFalseTotal
if ($this_row_matches) 0 0 0
268 {
269 0 $start_range = $i;
270 0 $match_found = 1;
271 0 $matches = 1;
272 }
273 }
274 # the end-range is always increasing so long
275 # as the row matches
276 0 ▶ if ($this_row_matches)
Branch: 1 branch
BranchTrueFalseTotal
if ($this_row_matches) 0 0 0
277 {
278 0 $end_range = $i;
279 }
280 # have we stopped matching? If so, stop looking.
281 0 ▶ if ($matches)
Branch: 1 branch
BranchTrueFalseTotal
if ($matches) 0 0 0
282 {
283 0 ▶ if (!$this_row_matches)
Branch: 1 branch
BranchTrueFalseTotal
unless ($this_row_matches) 0 0 0
284 {
285 0 $matches = 0;
286 0 last;
287 }
288 }
289 }
290 0 $self->{OPTIONS}->{start_rec} = $start_offset + $start_range;
291 0 $self->{OPTIONS}->{num_recs} = ($end_range - $start_range) + 1;
292 0 ▶ if (!$match_found)
Branch: 1 branch
BranchTrueFalseTotal
unless ($match_found) 0 0 0
293 {
294 0 $self->{OPTIONS}->{num_recs} = 0;
295 }
296 0 ▶ if ($DEBUG)
Branch: 1 branch
BranchTrueFalseTotal
if ($DEBUG) 0 0 0
297 {
298 0 print STDERR "set_sel_slice:";
299 0 print STDERR " start_rec=", $self->{OPTIONS}->{start_rec};
300 0 print STDERR " end_range=", $end_range;
301 0 print STDERR " num_recs=", $self->{OPTIONS}->{num_recs};
302 0 print STDERR "\n";
303 }
304 } # set_sel_slice
305  
306 =head2 clear_sel_slice
307  
308 Restore this selection to the full selection (if it has been
309 previously "sliced"). If it hasn't been previously sliced, then
310 calling this makes no difference.
311  
312 $arr_obj->clear_sel_slice();
313  
314 =cut
315 0 ▶sub clear_sel_slice {
Subroutine clear_sel_slice: not called CC=1 SCAR=6.9
Pod documented
316 0 my $self = shift;
317 0 my %args = (
318 @_
319 );
320  
321 0 $self->{OPTIONS}->{start_rec} = 0;
322 0 $self->{OPTIONS}->{num_recs} = scalar @{$self->{SEL_RECS}};
323 } # clear_sel_slice
324  
325 =head2 sort_records
326  
327 $sel->sort_records(
328 sort_by=>[qw(Author Series SeriesOrder Title Date)],
329 sort_numeric=>{ SeriesOrder=>1 },
330 sort_title=>{ Title=>1 },
331 sort_lastword=>{ Author=>1 },
332 sort_reversed=>{ Date=>1 });
333  
334 Take the current selected records array and sort it by field names.
335 The B<sort_by> array contains an array of field names for this data.
336 Yes, that's right, you can sort on multiple fields.
337  
338 The other arguments are for indications of changes to the type of sorting
339 done on the given fields.
340  
341 =over
342  
343 =item sort_numeric
344  
345 The given field(s) should be sorted as numbers.
346 Note that for multi-valued fields, this compares only the first value
347 in the set.
348  
349 =item sort_title
350  
351 The given field(s) should be treated as titles: any leading "The "
352 or "A " will be ignored.
353  
354 =item sort_lastword
355  
356 The given field(s) will be sorted with their last word first
357 (such as for surnames).
358  
359 =item sort_reversed
360  
361 The given field(s) will be sorted in reverse order.
362  
363 =back
364  
365 =cut
366 12 ▶sub sort_records ($%) {
Subroutine sort_records: called CC=29 SCAR=35.6
Pod documented
367 12 my $self = shift;
368 12 my %args = (
369 sort_by => undef,
370 sort_numeric => undef,
371 sort_reversed => undef,
372 sort_title => undef,
373 sort_lastword => undef,
374 @_
375 );
376 12 my $records_ref = $self->{SEL_RECS};
377  
378 12 my @sort_fields = @{$args{sort_by}};
379 12 my @sort_order = ();
380 my %sort_numerically = (defined $args{sort_numeric}
381 12 ▶ ? %{$args{sort_numeric}} : ());
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'sort_numeric'} ? : 3 9 12
382 my %sort_reversed = (defined $args{sort_reversed}
383 12 ▶ ? %{$args{sort_reversed}} : ());
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'sort_reversed'} ? : 5 7 12
384 12 ▶ my %sort_title = (defined $args{sort_title} ? %{$args{sort_title}} : ());
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'sort_title'} ? : 2 10 12
385 my %sort_lastword = (defined $args{sort_lastword}
386 12 ▶ ? %{$args{sort_lastword}} : ());
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'sort_lastword'} ? : 2 10 12
387 # filter out any illegal fields
388 12 foreach my $sfname (@sort_fields)
389 {
390 15 ▶ if (exists $self->{field_names_hash}->{$sfname}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"field_names_hash"}{$sfname} and defined $self->{"field_names_hash"}{$sfname} and $self->{"field_names_hash"}{$sfname}) 15 0 15
Condition33%2/6
exists $self->{"field_names_hash"}{$sfname} and defined $self->{"field_names_hash"}{$sfname} and $self->{"field_names_hash"}{$sfname}
!ll&&!rl&&r
0015
exists $self->{"field_names_hash"}{$sfname} and defined $self->{"field_names_hash"}{$sfname}
!ll&&!rl&&r
0015
MC/DC0%0/3
exists $self->{"field_names_hash"}{$sfname} and defined $self->{"field_names_hash"}{$sfname} and $self->{"field_names_hash"}{$sfname}
exists $self->{"field_names_hash"}{$sfname} defined $self->{"field_names_hash"}{$sfname} $self->{"field_names_hash"}{$sfname}
Truth table25%1/4
A and B and C
A= exists $self->{"field_names_hash"}{$sfname}B= defined $self->{"field_names_hash"}{$sfname}C= $self->{"field_names_hash"}{$sfname}
ABCresult
0XX0
10X0
1100
1111
391 && defined $self->{field_names_hash}->{$sfname}
392 && $self->{field_names_hash}->{$sfname})
393 {
394 15 push @sort_order, $sfname;
395 }
396 }
397  
398 # pre-cache the actual comparison values
399 12 my %values = ();
400 12 foreach my $rec (@{$records_ref})
401 {
402 289 my $a_row = $self->SUPER::FETCH($rec);
403 289 $values{$rec} = {};
404 289 foreach my $fn (@sort_order)
405 {
406 # allow for multi-valued fields
407 364 my @a_arr = @{$a_row->{\$fn}};
408 364 ▶ if (!@a_arr)
Branch: 1 branch
BranchTrueFalseTotal
if (not @a_arr) { } 0 364 364
409 {
410 0 ▶ if (defined $sort_numerically{$fn}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sort_numerically{$fn} and $sort_numerically{$fn}) { } 0 0 0
Condition0%0/3
defined $sort_numerically{$fn} and $sort_numerically{$fn}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $sort_numerically{$fn} and $sort_numerically{$fn}
defined $sort_numerically{$fn} $sort_numerically{$fn}
Truth table0%0/3
A and B
A= defined $sort_numerically{$fn}B= $sort_numerically{$fn}
ABresult
0X0
100
111
411 && $sort_numerically{$fn})
412 {
413 # sort undefined as zero
414 0 $values{$rec}->{$fn} = 0;
415 }
416 else
417 {
418 # sort undefined as the empty string
419 0 $values{$rec}->{$fn} = '';
420 }
421 }
422 else
423 {
424 364 my $a_val = '';
425 364 ▶ if (defined $sort_numerically{$fn}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sort_numerically{$fn} and $sort_numerically{$fn}) { } 31 333 364
Condition66%2/3
defined $sort_numerically{$fn} and $sort_numerically{$fn}
!ll&&!rl&&r
333031
MC/DC50%1/2
defined $sort_numerically{$fn} and $sort_numerically{$fn}
defined $sort_numerically{$fn} $sort_numerically{$fn}
Truth table66%2/3
A and B
A= defined $sort_numerically{$fn}B= $sort_numerically{$fn}
ABresult
0X0
100
111
426 && $sort_numerically{$fn})
427 {
428 # multi-valued fields don't make sense for
429 # numeric comparisons, so just take the first one
430 31 $a_val = $a_arr[0];
431 31 $a_val =~ s/\s//g; # remove any spaces
432 # non-numeric data should be compared as zero
433 31 ▶ if (!defined $a_val
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $a_val && $a_val or $a_val =~ /\D/) 6 25 31
Condition66%4/6
not defined $a_val && $a_val or $a_val =~ /\D/
l!l&&r!l&&!r
6025
defined $a_val && $a_val
!ll&&!rl&&r
0625
MC/DC33%1/3
not defined $a_val && $a_val or $a_val =~ /\D/
defined $a_val $a_val $a_val =~ /\D/
Truth table50%2/4
not(A && B) or C
A= defined $a_valB= $a_valC= $a_val =~ /\D/
ABCresult
0XX1
10X1
1111
1100
434 || !$a_val
435 || $a_val =~ /\D/)
436 {
437 6 $a_val = 0;
438 }
439 }
440 else # strings
441 {
442 # allow for titles
443 333 ▶ if ($sort_title{$fn})
Branch: 1 branch
BranchTrueFalseTotal
if ($sort_title{$fn}) 6 327 333
444 {
445 6 @a_arr = map { s/^(The\s+|A\s+)//; $_ } @a_arr;
446 }
447 # do lastword stuff
448 333 ▶ if ($sort_lastword{$fn})
Branch: 1 branch
BranchTrueFalseTotal
if ($sort_lastword{$fn}) 50 283 333
449 {
450 50 @a_arr = map { s/^(.*)\s+(\w+)$/$2,$1/; $_ } @a_arr;
451 }
452 333 $a_val = join('###', @a_arr);
453 }
454 364 $values{$rec}->{$fn} = $a_val;
455 }
456 }
457 }
458  
459 my @sorted_records = sort {
460 495 my $result = 0;
461 495 foreach my $fn (@sort_order)
462 {
463 551 my $a_val = $values{$a}->{$fn};
464 551 my $b_val = $values{$b}->{$fn};
465 $result =
466 (
467 (defined $sort_reversed{$fn} && $sort_reversed{$fn})
468 ? (
469 (defined $sort_numerically{$fn} && $sort_numerically{$fn})
470 ? ($b_val <=> $a_val)
471 : ($b_val cmp $a_val)
472 )
473 : (
474 551 ▶ (defined $sort_numerically{$fn} && $sort_numerically{$fn})
Branch: 3 branches
BranchTrueFalseTotal
defined $sort_reversed{$fn} && $sort_reversed{$fn} ? : 161 390 551
defined $sort_numerically{$fn} && $sort_numerically{$fn} ? : 84 77 161
defined $sort_numerically{$fn} && $sort_numerically{$fn} ? : 0 390 390
Condition55%5/9
defined $sort_reversed{$fn} and $sort_reversed{$fn}
!ll&&!rl&&r
3900161
defined $sort_numerically{$fn} and $sort_numerically{$fn}
!ll&&!rl&&r
77084
defined $sort_numerically{$fn} and $sort_numerically{$fn}
!ll&&!rl&&r
39000
MC/DC50%1/2
defined $sort_reversed{$fn} and $sort_reversed{$fn}
defined $sort_reversed{$fn} $sort_reversed{$fn}
MC/DC50%1/2
defined $sort_numerically{$fn} and $sort_numerically{$fn}
defined $sort_numerically{$fn} $sort_numerically{$fn}
MC/DC0%0/2
defined $sort_numerically{$fn} and $sort_numerically{$fn}
defined $sort_numerically{$fn} $sort_numerically{$fn}
Truth table66%2/3
A and B
A= defined $sort_reversed{$fn}B= $sort_reversed{$fn}
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= defined $sort_numerically{$fn}B= $sort_numerically{$fn}
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= defined $sort_numerically{$fn}B= $sort_numerically{$fn}
ABresult
0X0
100
111
475 ? ($a_val <=> $b_val)
476 : ($a_val cmp $b_val)
477 )
478 );
479 551 ▶ if ($result != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($result != 0) 407 144 551
480 {
481 407 return $result;
482 }
483 }
484 $result;
485 12 } @{$records_ref};
486  
487 12 @{$self->{SEL_RECS}} = @sorted_records;
488  
489 } # sort_records
490  
491 =head2 get_column
492  
493 Get the data from a column.
494  
495 my @col = $obj->get_column(field_name=>$field_name,
496 unique=>1);
497  
498 If unique is true, then duplicate values will be eliminated.
499  
500 This can be useful in operating on subsets of the selection, for example if
501 one has sorted on a field, then one gets the column data for that field,
502 with "unique" to true, then calls L</set_sel_slice> with each unique
503 value...
504  
505 =cut
506 0 ▶sub get_column ($%) {
Subroutine get_column: not called CC=4 SCAR=30.0
Pod documented
507 0 my $self = shift;
508 0 my %args = (
509 field_name =>'',
510 unique =>1,
511 @_
512 );
513  
514 0 my @col = ();
515 0 my %col_vals = ();
516 0 ▶ for (my $i=0; $i < @{$self->{SEL_RECS}}; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->{"SEL_RECS"};}) 0 0 0
517 {
518 0 my $vals_ref = $self->FETCH($i);
519 0 my $val = $vals_ref->{$args{field_name}};
520 0 ▶ if ($args{unique})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'unique'}) { } 0 0 0
521 {
522 0 ▶ if (!$col_vals{$val})
Branch: 1 branch
BranchTrueFalseTotal
unless ($col_vals{$val}) 0 0 0
523 {
524 0 push @col, $val;
525 }
526 }
527 else
528 {
529 0 push @col, $val;
530 }
531 0 $col_vals{$val} = 1;
532 }
533 0 return @col;
534 } # get_column
535  
536 #================================================================
537 # Tie-Array interface
538  
539 =head1 TIE-ARRAY METHODS
540  
541 =head2 TIEARRAY
542  
543 Create a new instance of the object as tied to an array.
544  
545 tie @SEL_RECS, 'Tie::FieldVals::Select',
546 datafile=>$datafile, selection=>{$key=>$value},
547 match_any=>$val_any;
548  
549 The selection and match_any options are the selection criteria
550 used to define this sub-set; they have the same format as
551 those used in L<Tie::FieldVals::Row/match> and
552 L<Tie::FieldVals::Row/match_any> methods.
553  
554 Other options are the same as for L<Tie::FieldVals/TIEARRAY>.
555  
556 =cut
557 8 ▶sub TIEARRAY {
Subroutine TIEARRAY: called CC=3 SCAR=11.0
558 8 my $class = shift;
559  
560 # make the parent tie
561 8 my $self = Tie::FieldVals::TIEARRAY($class, @_);
562 # now, FILE_OBJ, FILE_RECS, OPTIONS, REC_CACHE, field_names
563 # field_names_hash
564 # should be set
565
566 # set the default additional options
567 8 ▶ $self->{OPTIONS}->{selection} ||= undef;
Condition100%2/2
$self->{"OPTIONS"}{"selection"} ||= undef
l!l
53
MC/DC100%1/1
$self->{"OPTIONS"}{"selection"} ||= undef
$self->{"OPTIONS"}{"selection"}
Truth table100%2/2
A
A= $self->{"OPTIONS"}{"selection"}
Aresult
11
00
568 8 ▶ $self->{OPTIONS}->{match_any} ||= undef;
Condition50%1/2
$self->{"OPTIONS"}{"match_any"} ||= undef
l!l
08
MC/DC0%0/1
$self->{"OPTIONS"}{"match_any"} ||= undef
$self->{"OPTIONS"}{"match_any"}
Truth table50%1/2
A
A= $self->{"OPTIONS"}{"match_any"}
Aresult
11
00
569  
570 # now, apply the selection to the records
571 8 $self->make_selection(%{$self->{OPTIONS}});
572  
573 8 return $self;
574 } # TIEARRAY
575  
576 =head2 FETCH
577  
578 Get a row from the array.
579  
580 $val = $array[$ind];
581  
582 Returns a reference to a Tie::FieldVals::Row hash, or undef.
583  
584 =cut
585 358 ▶sub FETCH {
Subroutine FETCH: called CC=5 SCAR=18.4
586 358 ▶ carp &whowasi if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 358 358
587 358 my ($self, $ind) = @_;
588  
589 358 ▶ if ($ind >= 0 && $ind < $self->{OPTIONS}->{num_recs})
Branch: 1 branch
BranchTrueFalseTotal
if ($ind >= 0 and $ind < $self->{"OPTIONS"}{"num_recs"}) 357 1 358
Condition66%2/3
$ind >= 0 and $ind < $self->{"OPTIONS"}{"num_recs"}
!ll&&!rl&&r
01357
MC/DC50%1/2
$ind >= 0 and $ind < $self->{"OPTIONS"}{"num_recs"}
$ind >= 0 $ind < $self->{"OPTIONS"}{"num_recs"}
Truth table66%2/3
A and B
A= $ind >= 0B= $ind < $self->{"OPTIONS"}{"num_recs"}
ABresult
0X0
100
111
590 {
591 357 my $s_ind = $ind + $self->{OPTIONS}->{start_rec};
592 357 my $real_ind = ${$self->{SEL_RECS}}[$s_ind];
593 357 ▶ if ($DEBUG)
Branch: 1 branch
BranchTrueFalseTotal
if ($DEBUG) 0 357 357
594 {
595 0 print STDERR "ind=$ind";
596 0 print STDERR " s_ind=$s_ind";
597 0 print STDERR " real_ind=$real_ind";
598 0 print STDERR "\n";
599 0 print STDERR Dumper(${$self->{FILE_RECS}}[$real_ind]);
600 }
601 357 return $self->SUPER::FETCH($real_ind);
602 }
603 1 return undef;
604 } # FETCH
605  
606 =head2 STORE
607  
608 Set a value in the array.
609  
610 $array[$ind] = $val;
611  
612 If $ind is bigger than the array, then do nothing. (If you want to add a
613 new row to the data file, do it directly with the Tie::FieldVals array.)
614 The $val is expected to be a Tie::FieldVals::Row hash.
615 This I<does> replace the given the data in the data file.
616  
617 =cut
618 0 ▶sub STORE {
Subroutine STORE: not called CC=4 SCAR=30.0
619 0 ▶ carp &whowasi if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 0 0
620 0 my ($self, $ind, $val) = @_;
621  
622 0 ▶ if ($ind >= 0 && $ind < $self->{OPTIONS}->{num_recs})
Branch: 1 branch
BranchTrueFalseTotal
if ($ind >= 0 and $ind < $self->{"OPTIONS"}{"num_recs"}) 0 0 0
Condition0%0/3
$ind >= 0 and $ind < $self->{"OPTIONS"}{"num_recs"}
!ll&&!rl&&r
000
MC/DC0%0/2
$ind >= 0 and $ind < $self->{"OPTIONS"}{"num_recs"}
$ind >= 0 $ind < $self->{"OPTIONS"}{"num_recs"}
Truth table0%0/3
A and B
A= $ind >= 0B= $ind < $self->{"OPTIONS"}{"num_recs"}
ABresult
0X0
100
111
623 {
624 0 my $s_ind = $ind + $self->{OPTIONS}->{start_rec};
625 0 my $real_ind = ${$self->{SEL_RECS}}[$s_ind];
626 0 $self->SUPER::STORE($real_ind, $val);
627 }
628 } # STORE
629  
630 =head2 FETCHSIZE
631  
632 Get the apparent size of the array. This gives the
633 size of the current slice, not the size of the underlying
634 array. Of course if we are not in "slice" mode, the two values
635 will be the same.
636  
637 =cut
638 287 ▶sub FETCHSIZE {
Subroutine FETCHSIZE: called CC=2 SCAR=7.1
639 287 ▶ carp &whowasi if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 287 287
640 287 my $self = shift;
641  
642 287 return $self->{OPTIONS}->{num_recs};
643 } # FETCHSIZE
644  
645 =head2 STORESIZE
646  
647 Set the apparent size of the array.
648 This actually sets the size of the current slice of the array,
649 not the underlying array.
650  
651 =cut
652 0 ▶sub STORESIZE {
Subroutine STORESIZE: not called CC=3 SCAR=24.8
653 0 ▶ carp &whowasi if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 0 0
654 0 my $self = shift;
655 0 my $count = shift;
656  
657 0 ▶ if ($count <= @{$self->{SEL_RECS}})
Branch: 1 branch
BranchTrueFalseTotal
if ($count <= @{$self->{"SEL_RECS"};}) 0 0 0
658 {
659 0 $self->{OPTIONS}->{num_recs} = $count;
660 }
661 } # STORESIZE
662  
663 =head2 EXISTS
664  
665 exists $array[$ind];
666  
667 Note that if the array is in "slice" mode, this will only
668 say whether the row exists in the slice.
669  
670 =cut
671 0 ▶sub EXISTS {
Subroutine EXISTS: not called CC=4 SCAR=30.0
672 0 ▶ carp &whowasi if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 0 0
673 0 my $self = shift;
674 0 my $ind = shift;
675  
676 0 ▶ if ($ind >= 0 && $ind < $self->{OPTIONS}->{num_recs})
Branch: 1 branch
BranchTrueFalseTotal
if ($ind >= 0 and $ind < $self->{"OPTIONS"}{"num_recs"}) 0 0 0
Condition0%0/3
$ind >= 0 and $ind < $self->{"OPTIONS"}{"num_recs"}
!ll&&!rl&&r
000
MC/DC0%0/2
$ind >= 0 and $ind < $self->{"OPTIONS"}{"num_recs"}
$ind >= 0 $ind < $self->{"OPTIONS"}{"num_recs"}
Truth table0%0/3
A and B
A= $ind >= 0B= $ind < $self->{"OPTIONS"}{"num_recs"}
ABresult
0X0
100
111
677 {
678 0 my $s_ind = $ind + $self->{OPTIONS}->{start_rec};
679 0 my $real_ind = ${$self->{SEL_RECS}}[$s_ind];
680 0 return $self->SUPER::EXISTS($real_ind);
681 }
682 0 return 0;
683 } # EXISTS
684  
685 =head2 DELETE
686  
687 delete $array[$ind];
688  
689 Delete the value at $ind -- deletes from the selection.
690 Does not delete from the data file.
691  
692 =cut
693 0 ▶sub DELETE {
Subroutine DELETE: not called CC=4 SCAR=30.0
694 0 ▶ carp &whowasi if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 0 0
695 0 my $self = shift;
696 0 my $ind = shift;
697  
698 0 ▶ if ($ind >= 0 && $ind < $self->{OPTIONS}->{num_recs})
Branch: 1 branch
BranchTrueFalseTotal
if ($ind >= 0 and $ind < $self->{"OPTIONS"}{"num_recs"}) 0 0 0
Condition0%0/3
$ind >= 0 and $ind < $self->{"OPTIONS"}{"num_recs"}
!ll&&!rl&&r
000
MC/DC0%0/2
$ind >= 0 and $ind < $self->{"OPTIONS"}{"num_recs"}
$ind >= 0 $ind < $self->{"OPTIONS"}{"num_recs"}
Truth table0%0/3
A and B
A= $ind >= 0B= $ind < $self->{"OPTIONS"}{"num_recs"}
ABresult
0X0
100
111
699 {
700 0 my $s_ind = $ind + $self->{OPTIONS}->{start_rec};
701 0 delete ${$self->{SEL_RECS}}[$s_ind];
702 0 $self->{OPTIONS}->{num_recs}--;
703 }
704 } # DELETE
705  
706 =head2 CLEAR
707  
708 @array = ();
709  
710 Clear the array -- clears the selection.
711 Does not affect the data file.
712  
713 =cut
714 0 ▶sub CLEAR {
Subroutine CLEAR: not called CC=2 SCAR=17.9
715 0 ▶ carp &whowasi if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 0 0
716 0 my $self = shift;
717 0 my $ind = shift;
718  
719 0 @{$self->{SEL_RECS}} = ();
720 0 $self->{OPTIONS}->{start_rec} = 0;
721 0 $self->{OPTIONS}->{num_recs} = 0;
722 } # CLEAR
723  
724 =head2 UNTIE
725  
726 untie @array;
727  
728 Untie the array.
729  
730 =cut
731 1 ▶sub UNTIE {
Subroutine UNTIE: called CC=3 SCAR=11.1
732 1 ▶ carp &whowasi if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 1 1
733 1 my $self = shift;
734 1 my $count = shift;
735  
736 1 ▶ carp "untie attempted while $count inner references still exist" if $count;
Branch: 1 branch
BranchTrueFalseTotal
if $count 0 1 1
737 1 $self->{SEL_RECS} = [];
738 1 $self->{OPTIONS}->{start_rec} = 0;
739 1 $self->{OPTIONS}->{num_recs} = 0;
740 1 $self->SUPER::UNTIE($count);
741 } # UNTIE
742  
743 =head1 PRIVATE METHODS
744  
745 For developer reference only.
746  
747 =head2 debug
748  
749 Set debugging on.
750  
751 =cut
752 1 ▶sub debug { $DEBUG = @_ ? shift : 1 }
Subroutine debug: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 0 1
753  
754 =head2 whowasi
755  
756 For debugging: say who called this
757  
758 =cut
759 0 ▶sub whowasi { (caller(1))[3] . '()' }
Subroutine whowasi: not called CC=1 SCAR=6.9
Pod documented
760  
761 =head1 REQUIRES
762  
763 Test::More
764 Carp
765 Data::Dumper
766 Fcntl
767 Tie::FieldVals
768 Tie::FieldVals::Row
769  
770 =head1 SEE ALSO
771  
772 perl(1).
773 L<Tie::FieldVals>
774 L<Tie::FieldVals::Row>
775  
776 =head1 BUGS
777  
778 Please report any bugs or feature requests to the author.
779  
780 =head1 AUTHOR
781  
782 Kathryn Andersen (RUBYKAT)
783 perlkat AT katspace dot com
784 http://www.katspace.com
785  
786 =head1 COPYRIGHT AND LICENCE
787  
788 Copyright (c) 2004 by Kathryn Andersen
789  
790 This program is free software; you can redistribute it and/or modify it
791 under the same terms as Perl itself.
792  
793  
794 =cut
795  
796 8 1; # End of Tie::FieldVals::Select
797 # vim: ts=8 sts=4 sw=4
798 __END__
↑ Summary blib/lib/Tie/FieldVals/Row.pm »