Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-04-16 12:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found