Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

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

The memory consumption of this is fixed to the total size of the domain regardless of the number of subranges it contains. Construction and addition of ranges is trivial. Lookup is O(1).

If the total range is somewhere in the bounds of sanity, then this is quick and easy and will often use less memory than the related hash or tree structure.

If the range is large, then you can use vec and reduce the memory requirement to 1/8 th.

It doesn't work well for real numbers--though it can approximate them.

#! perl -slw use strict; use List::Util qw[ reduce ]; $a = $b; my $zones = reduce { my( $first, $last ) = split '-', $b; $last ||= $first; ## 1; corrected per [bmann]'s post below. substr( $a, $first, $last - $first + 1 ) = 'x' x ( $last - $first ++ 1 ); $a } chr(0) x 1000, split ',', '10-374,376-379,382-385,388-499,530-534,541-543,618,619,700-704,707-70 +9'; print $zones =~ m[^.{$_}x] ? "Found $_!" : "$_ isn't there!" for 9, 374, 375, 376, 999; __END__ [12:03:42.74] P:\test>425464 9 isn't there! Found 374! 375 isn't there! Found 376! 999 isn't there!

Examine what is said, not who speaks.
Silence betokens consent.
Love the truth but pardon error.

In reply to Re: Matching data against non-consecutive range by BrowserUk
in thread Matching data against non-consecutive range by Popcorn Dave

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 having a coffee break in the Monastery: (5)
As of 2024-04-23 19:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found