| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Number::Closest; |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
|
4
|
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
43335
|
use Moose; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
has 'number' => (isa => 'Num', is => 'rw', required => 1); |
|
8
|
|
|
|
|
|
|
has 'numbers' => (isa => 'ArrayRef[Num]', is => 'rw', required => 1); |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub analyze { |
|
11
|
|
|
|
|
|
|
my($self)=@_; |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
my @dist = sort { $a->[1] <=> $b->[1] } map { [$_, abs($_ - $self->number)] } @{$self->numbers} ; |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
\@dist |
|
16
|
|
|
|
|
|
|
} |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub find { |
|
19
|
|
|
|
|
|
|
my($self,$amount)=@_; |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
$amount ||= 1; |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
my $list = $self->analyze; |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
my $n = $amount <= scalar @$list ? $amount : scalar @$list ; |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
#warn "N: $n"; |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
my @closest = @{$list}[0..($n-1)] ; |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
my @c = map { $_->[0] } @closest; |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
#use Data::Dumper; |
|
34
|
|
|
|
|
|
|
#warn Dumper \@closest, \@c; |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
if ($amount == 1) { |
|
37
|
|
|
|
|
|
|
$c[0] ; |
|
38
|
|
|
|
|
|
|
} else { |
|
39
|
|
|
|
|
|
|
\@c; |
|
40
|
|
|
|
|
|
|
} |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
} |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head1 NAME |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
Number::Closest - find number(s) closest to a number in a list of numbers |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
use Number::Closest; |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
my $finder = Number::Closest->new(number => $num, numbers => \@num) ; |
|
56
|
|
|
|
|
|
|
my $closest = $finder->find; # finds closest number |
|
57
|
|
|
|
|
|
|
my $closest_two = $finder->find(2) ; # gives arrayref of two closest numbers in list |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# or, all in one shot |
|
60
|
|
|
|
|
|
|
Number::Closest->new(number => $num, numbers => \@num)->find(1) ; |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
L<http://stackoverflow.com/questions/445782/finding-closest-match-in-collection-of-numbers> |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=head1 AUTHOR |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
Terrence Brannon, C<< <tbone at cpan.org> >> |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head1 BUGS |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
Please report any bugs or feature requests to C<bug-number-closest at rt.cpan.org>, or through |
|
77
|
|
|
|
|
|
|
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Number-Closest>. I will be notified, and then you'll |
|
78
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=head1 SUPPORT |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
perldoc Number::Closest |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
You can also look for information at: |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=over 4 |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Number-Closest> |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
L<http://annocpan.org/dist/Number-Closest> |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=item * CPAN Ratings |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
L<http://cpanratings.perl.org/d/Number-Closest> |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=item * Search CPAN |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
L<http://search.cpan.org/dist/Number-Closest/> |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=back |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=begin html |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
<img src="http://dev.mysql.com/doc/sakila/en/images/sakila-schema.png"> |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=end html |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
Copyright 2009 Terrence Brannon, all rights reserved. |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
|
126
|
|
|
|
|
|
|
under the same terms as Perl itself. |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=cut |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
1; # End of Number::Closest |