http://www.perlmonks.org?node_id=11106887


in reply to Find range in array with indices

Tk::Text returns indices, not coordinates. The number before the dot is the line, the number after the dot is the character. In this respect, the indices are similar to v-strings (v7.23 lt v7.133).

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.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: Find range in coordinates array
by IB2017 (Pilgrim) on Oct 01, 2019 at 20:50 UTC

    Thank you choroba. Your solution seems to work just fine with my real data.