in reply to Find range in array with indices

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" }

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

    Thank you so much. This is such a beautiful implementation!

    It works very well if my search value has only 2 digits after the ".". I cannot match anything if I search for the following value with 3 digits after the "." (sorry I haven't specified the forms it can take). Is it possible to make your solution more flexible in terms of number of digits?

    my $input = 7.255;
      I cannot match anything if I search for the following value with 3 digits after the "."

      With the code I showed, it works for me for values such as my $input = 7.111; (that are actually in the range 7.023-7.133). So if that's not working for you, perhaps you could show an SSCCE?

      If you're getting the "Input too wide" error, then probably your @array only contains values with two digits after the decimal point or less and the code is adapting $maxlen (the maximum number of digits after the decimal) to that automatically. You could also do something like "$maxlen should always be at least three digits, or one digit longer than the values in @array, whichever is bigger" by saying my $maxlen = 1 + max 2, map ..., or you could just use a fixed $maxlen.

        I had a closer look at your code. Even if there are some aspects I still do not quite understand, I can confirm that it works brilliantly, even better than I first thought. I have tested it with real (a lot of) data. Simply perfect.