# Find closest value - PERL Beginners

Hello, is it possible, with perl, to find the closest numerical value to a set value. IE. a set value of 15 and I have five values, 208,258,56,123 is there a function too go through the five values array to find the closest to 15 ?? TIA, -- Mike<mickalo>Blezien =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= Thunder Rain Internet Publishing Providing Internet Solutions that work! http://www.thunder-rain.com =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=...

1. ## Find closest value

Hello,

is it possible, with perl, to find the closest numerical value to a set value.
IE. a set value of 15 and I have five values, 208,258,56,123

is there a function too go through the five values array to find the closest to
15 ??

TIA,
--
Mike<mickalo>Blezien
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Thunder Rain Internet Publishing
Providing Internet Solutions that work!
http://www.thunder-rain.com
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Mike Guest

2. ## Re: Find closest value

On 5/16/2004 12:50 PM, Mike Blezien wrote:

If it's an unsorted list and you only need to search once, your best bet
is to walk the list. OTOH if your going to be searching multiple times,
you will be better off to sort the list and then use a binary search
algorithm.

Randy.

Randy Guest

3. ## Re: Find closest value

----- Original Message -----
From: "Mike Blezien" <net>
Newsgroups: perl.beginners
To: "Perl List" <org>
Sent: Sunday, May 16, 2004 12:50 PM
Subject: Find closest value

value.
closest to

Hi Mike
Doing a quick search on CPAN didn't turn up anything, (as far as I can
tell),
but the following code will do what you want, I believe, but is not a
function.

Chris

#!/usr/bin/perl
use strict;
use warnings;

my \$set = 15;
my vals = (208,258,56,123);

my (\$min_idx) = map{\$_->[0]}
sort{ \$a->[1] <=> \$b->[1]}
map{[\$_, abs(\$vals[\$_]-\$set)]} 0..\$#vals;

print "index of element closest to \$set is \$min_idx",
" with value \$vals[\$min_idx]\n";

__END__
*** Output ***
index of element closest to 15 is 2 with value 56

Chris Guest

4. ## Re: Find closest value

Mike Blezien wrote:

Hi Mike.

There's no built-in function, but it's simple to write one; the code below will
do what you want. It's not particularly efficient, but is more than likely
to be fast enough for you unless your list of values is huge.

HTH,

Rob

use strict;
use warnings;

my \$set = 15;
my vals = (208, 258, 56, 123);

print closest(\$set, vals), "\n";

sub closest {
my \$val = shift;
my list = sort { abs(\$a - \$val) <=> abs(\$b - \$val) } _;
\$list[0];
}

**OUTPUT

56

Rob Guest

5. ## Re: Find closest value

Thx's Rob,... and the others who responded.

It gave me some good ideas to persue and I think I have what I was trying to
accomplish now. Just need to do some fine tuning :)

Appreciate the help as always.

Mike<mickalo>Blezien
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Thunder Rain Internet Publishing
Providing Internet Solutions that work!
http://www.thunder-rain.com
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Rob Dixon wrote:
>
>
> Hi Mike.
>
> There's no built-in function, but it's simple to write one; the code below will
> do what you want. It's not particularly efficient, but is more than likely
> to be fast enough for you unless your list of values is huge.
>
> HTH,
>
> Rob
>
>
> use strict;
> use warnings;
>
> my \$set = 15;
> my vals = (208, 258, 56, 123);
>
> print closest(\$set, vals), "\n";
>
> sub closest {
> my \$val = shift;
> my list = sort { abs(\$a - \$val) <=> abs(\$b - \$val) } _;
> \$list[0];
> }
>
> **OUTPUT
>
> 56
>
>
>[/ref]

Mike Guest

6. ## RE: Find closest value

Mike Blezien wrote:

Here's an approach that doesn't use sorting:

#!/usr/bin/perl

print "closest=", closest(15, 208, 258, 56, 123), "\n";

sub closest {
my \$find = shift;
my \$closest = shift;
abs(\$_ - \$find) < abs(\$closest - \$find) and \$closest = \$_ for _;
\$closest;
}
Bob Guest

7. ## RE: Find closest value

Bob Showalter wrote:
>
> Here's an approach that doesn't use sorting:
>
> #!/usr/bin/perl
>
> print "closest=", closest(15, 208, 258, 56, 123), "\n";
>
> sub closest {
> my \$find = shift;
> my \$closest = shift;
> abs(\$_ - \$find) < abs(\$closest - \$find) and \$closest = \$_ for
> _; \$closest;
> }[/ref]

And in a more readable fashion:

sub closest
{
my \$find = shift;
my \$closest = shift;
for my \$num (_)
{
if (abs(\$num - \$find) < abs(\$closest - \$find))
{
\$closest = \$num;
}
}
return \$closest;
}
Luke Guest

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•