If I understand your format correctly, that's a dangerous way to store these coordinates, as floating-point inaccuracies might possibly mess them up. Is there any way for you to change their format to at least be strings? And what is the maximum number of digits after the decimal point that you can expect?

One possible solution is normalizing the values into integers, then you can use a module such as Set::IntSpan. The following method is fairly expensive in setup, but in exchange lookups should be fairly fast.

<update> Here's a somewhat less complicated version that does pretty much the same. It doesn't overwrite the input array with the "normalized" values and convert them back after, it just keeps the original array.

use warnings; use strict; use List::Util qw/max pairmap/; use Set::IntSpan; my @array = (1.1,1.4,5.33,6.1,7.23,7.133,10,11); my $input = 7.25; # check the maxmum number of digits after the decimal point my $maxlen = max map { length( (split /\./,$_,2)[1]//'' ) } @array; # build the set my $set = Set::IntSpan->new( [ pairmap { [$a,$b] } map { my @x = split /\./, $_, 2; sprintf "%d%0*d", $x[0], $maxlen, $x[1]//0 } @array ] ); # normalize the input die "Input too wide" unless $input=~/\A\d+(\.\d{1,$maxlen})?\z/; my $innorm = do { my @x = split /\./, $input, 2; sprintf "%d%0*d", $x[0], $maxlen, $x[1]//0 }; # lookup if ( defined( my $ord = $set->span_ord( $innorm ) ) ) { my ($x,$y) = @array[ $ord*2, $ord*2+1 ]; print "found $input in span $x-$y\n"; } else { warn "did not find $input in any of the spans\n" }

</update>

Original code:

use warnings; use strict; use Data::Dump; use List::Util qw/max pairmap/; use Set::IntSpan; my @array = (1.1,1.4,5.33,6.1,7.23,7.133,10,11); my $input = 7.25; # "normalize" the input (could also use fixed @maxlen) my @maxlen = map { my $x=$_; max map { length $$_[$x] } map { my @e=split /\./,$_,2; @e>1 ? \@e : [@e,0] } @array } 0,1; my $normalize = sub { my ($s) = @_; die "Invalid: $s" unless $s=~/\A\d{1,$maxlen[0]}(\.\d{1,$maxlen[1]})?\z/; my ($x,$y) = split /\./, $s, 2; 0+sprintf "%*d%0*d", $maxlen[0], $x, $maxlen[1], $y//0 }; my $denormalize = sub { my ($n) = @_; die "Too long: $n" if length($n)>$maxlen[0]+$maxlen[1]; my $y = 0+substr $n, -$maxlen[1], $maxlen[1], ''; return (0+($n||0)).($y?".$y":'') }; dd map { $normalize->($_) } @array; # Debug # => "(1001, 1004, 5033, 6001, 7023, 7133, 10000, 11000)" dd map { $denormalize->($normalize->($_)) } @array; # => "(1.1, 1.4, 5.33, 6.1, 7.23, 7.133, 10, 11)" $denormalize->($normalize->($_)) eq $_ or die "$_" for @array; # Test my $set = Set::IntSpan->new( [ pairmap { [$a,$b] } map { $normalize->($_) } @array ] ); print $set->run_list, "\n"; # Debug # => "1001-1004,5033-6001,7023-7133,10000-11000" if ( defined( my $ord = $set->span_ord( $normalize->($input) ) ) ) { my $span = ($set->spans)[$ord]; print "found $input in span ", join( '-', map { $denormalize->($_) } @$span ), "\n"; # => "found 7.25 in span 7.23-7.133" } else { warn "did not find $input in any of the spans\n" }

In reply to Re: Find range in coordinates array (updated) by haukex
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.