in reply to retrive data from another file by comparing the values
You could use List::Util::first:
which produces: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 appears to be per "specification".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
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
|
|---|