Professional Web Applications Themes

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. #1

    Default 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. #2

    Default 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. #3

    Default 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. #4

    Default 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. #5

    Default 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. #6

    Default 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. #7

    Default 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

Similar Threads

  1. Measuring closest distance
    By magnus_andersson in forum Macromedia Director 3D
    Replies: 4
    Last Post: February 13th, 01:36 PM
  2. Querying a date closest to today
    By Explorer5 in forum Coldfusion Database Access
    Replies: 2
    Last Post: February 24th, 07:53 PM
  3. SUPER-UX Cray - what is the closest popular unix version ?
    By Jean-Pierre Denis in forum Linux / Unix Administration
    Replies: 6
    Last Post: May 4th, 06:06 AM
  4. Converting RGB colors to closest Pantone
    By David J in forum Macromedia Freehand
    Replies: 1
    Last Post: March 7th, 11:46 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139