in reply to Re: Find range in coordinates array (updated)
in thread Find range in array with indices

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;

Replies are listed 'Best First'.
Re^3: Find range in coordinates array
by haukex (Archbishop) on Oct 01, 2019 at 18:29 UTC
    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.

        Glad to help, and please feel free to ask :-) You can add the same debugging statements as in my first piece of code (at the bottom of the post), then you can hopefully see how everything is being transformed. If I were to nitpick my own code, I would probably add some checking that all the inputs are in the expected formats (e.g. use a regex instead of split). And the updated piece of code depends on the original array not changing, since it creates a second list of indicies.