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]

In reply to Re: Find range in coordinates array by choroba
in thread Find range in array with indices by IB2017

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.