You need to do the comparison yourself:
#!/usr/bin/perl use warnings; use strict; my @indices = qw( 1.1 1.4 5.33 6.1 7.23 7.133 10 11 ); sub i_le { my ($x, $y) = @_; my ($x_line, $x_char) = split /\./, $x; my ($y_line, $y_char) = split /\./, $y; $_ //= 0 for $x_char, $y_char; return $x_line < $y_line || $x_line == $y_line && $x_char <= $y_char } sub range { my ($in) = @_; for my $i (0 .. $#indices / 2) { return @indices[$i * 2, $i * 2 + 1] if i_le($indices[ $i * 2 ], $in) && i_le($in, $indices[ $i * 2 + 1 ]); } } use Test::More tests => 5; is_deeply [range('1.1')], [qw[ 1.1 1.4 ]]; is_deeply [range('1.2')], [qw[ 1.1 1.4 ]]; is_deeply [range('1.4')], [qw[ 1.1 1.4 ]]; is_deeply [range('7.50')], [qw[ 7.23 7.133 ]]; is_deeply [range('10.5')], [qw[ 10 11 ]];
This is suboptimal as the input is split again for each comparison. It might be better to store tuples instead of strings, i.e. convert on input to tuples and convert back to indices on output.
Note that I used strings to store the indices to avoid imprecise floating point arithmetic.
In reply to Re: Find range in coordinates array
by choroba
in thread Find range in array with indices
by IB2017
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |