You could use List::Util::first:

use strict ; use warnings ; use List::Util qw(first) ; my @table = () ; while (<DATA>) { m/^\s*(\S+)\s+(\S+)\s*$/ ; push @table, [$1, $2] ; } ; @table = reverse sort { $a->[1] <=> $b->[1] ; } @table ; my @test = (2, 2.69, 2.7, 3.9, 50, 51, 51.1, 200) ; my @ok = (0, 1, 1, 5, 7, 170, 171, 171) ; foreach my $t (@test) { my $r = first { $_->[1] <= $t } @table ; $r = defined($r) ? $r->[0] : 0 ; my $o = shift(@ok) ; printf "%7.3f -> %4d %s\n", $t, $r, $r == $o ? "OK" : "??? expected + $o" ; } ; __DATA__ 1 2.69 2 2.97032 3 3.25064 4 3.53096 5 3.81128 6 4.0916 7 4.37192 170 50.98 171 51.086
which produces:
2.000 -> 0 OK 2.690 -> 1 OK 2.700 -> 1 OK 3.900 -> 5 OK 50.000 -> 7 OK 51.000 -> 170 OK 51.100 -> 171 OK 200.000 -> 171 OK
which appears to be per "specification".

Though you could throw a binary chop at it, if the table is large and it's going to be used a lot:

use strict ; use warnings ; my @table = () ; while (<DATA>) { m/^\s*(\S+)\s+(\S+)\s*$/ ; push @table, [$1, $2] ; } ; @table = sort { $a->[1] <=> $b->[1] ; } @table ; my @test = (2, 2.69, 2.7, 3.9, 50, 51, 51.1, 200) ; my @ok = (0, 1, 1, 5, 7, 170, 171, 171) ; foreach my $t (@test) { my $r = whack(\@table, $t) ; $r = defined($r) ? $r->[0] : 0 ; my $o = shift(@ok) ; printf "%7.3f -> %4d %s\n", $t, $r, $r == $o ? "OK" : "??? expected + $o" ; } ; sub whack { my ($rt, $v) = @_ ; my $t = $rt->[0] ; if ($t->[1] > $v) { return undef ; } # quit if not in table my $h = $#$rt ; my $l = 0 ; my $i ; while ($l < $h) { $t = $rt->[$i = ($l + $h + 1) >> 1] ; # new mid index and value if ($t->[1] <= $v) { last if $t->[1] == $v ; # quit if exact match $l = $i ; # entry[$i] < $v -- move sear +ch up } else { $h = --$i ; # entry[$i] > $v -- move sear +ch down past it } ; } ; return $rt->[$i] ; } ; __DATA__ 1 2.69 2 2.97032 3 3.25064 4 3.53096 5 3.81128 6 4.0916 7 4.37192 170 50.98 171 51.086

In reply to Re: retrive data from another file by comparing the values by gone2015
in thread retrive data from another file by comparing the values by amshi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.