Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Here's the pod...

SYNOPSIS

use Set::Range;
my %set= ('1' => { lower => 0, upper => 10, upper_inclusive => 1, }, '2' => { lower => 11, upper => 100, lower_inclusive => 1, }, );
my $range=Set::Range->new(\%set); print $range->getState('19', NUM_RANGE); # prints "2"
my %set= ('Jan' => { lower => '1/1/2001', upper => '1/31/2001', upper_inclusive => 1, lower_inclusive => 1, }, # Days in January 'Feb' => { lower => '2/1/2001', upper => '2/28/2001', upper_inclusive => 1, lower_inclusive => 1, }, # Days in February );
my $range=Set::Range->new(\%set);
print $range->getSet('1/11/2001', DATE_RANGE); # prints "Jan"

DESCRIPTION

Set::Range allows you to define ranges of numeric or date values and will return the set that a given test value lies in. This module removes the need for multiple if .. elsif .. (etc) tests and lets you modify the sets on the fly without having to recode the logic in your scripts.

E.g.: define date ranges and test for which set '1/1/2002' is in.

METHODS

new
This creates the Set::Range Object

Example: my $range = Set::Range->new(\%sets);

Pass new() a reference to a Hash containing the set information.

%sets
The hash defining the sets contains one hash per set with at least 'upper' and 'lower' defined. 'upper_inclusive' and 'lower_inclusive' are optional and are the equivalent of >= and <= for the upper and lower set boundries.
The set hash can look like this:
{ 'key1' => { lower => 0, upper => 10 }, 'key2' => { lower => 10, upper => 15, lower_inclusive => 1, }, 'key3' => { lower => 15 upper => 25, lower_inclusive => 1, upper_inclusive => 1, }, }
etc... The lower and upper values can either be numeric values or date values in the form mm/dd/yyyy or dd/mm/yyyy (see C<getSet> for Euro-formatted dates)
getSet
getSet() returns the value of the set that the test item is a member of. Optionally accepts one of the following contants (defaults to numeric): NUM_RANGE, DATE_RANGE, EU_DATE_RANGE, TIME_RANGE(same as NUM_RANGE)

Examples:

my $set = $range->getSet(10); my $set = $range->getSet('1/14/2002', DATE_RANGE);

getSet returns 0 if the test value was not found to be a member of any set.

Note: Set::Range uses Date::Calc functions to convert the date to a datestamp. I settled on Date::Calc because Time::Local doesn't support years > 2038.

EXPORT

NUM_RANGE, DATE_RANGE, EU_DATE_RANGE, TIME_RANGE constants


In reply to Re: Set::Range - conditional testing on sets by $code or die
in thread Set::Range - conditional testing on sets by $code or die

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 chilling in the Monastery: (4)
As of 2024-04-20 04:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found