Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Set::Range - conditional testing on sets

by japhy (Canon)
on Apr 30, 2001 at 04:16 UTC ( [id://76531]=note: print w/replies, xml ) Need Help??


in reply to Set::Range - conditional testing on sets

Here's my implementation. Documentation will come in a while, and I haven't tested it (I just wrote it, and I'm in the mood for dinner), but here's the code:
package Set::Range; $VERSION = '1.0j'; # j for japhy ;) sub new { my $class = shift; my $self = bless { CACHE => {}, RANGE => [], }, $class; while (my ($k,$v) = splice(@_, 0, 2)) { my ($low, $high) = split /\s*(?:,|\.{2,})\s*/, $k; unless ($high) { $high = substr($low, 1); chop $low; } my $li = (substr($low, 0, 1, '') eq '['); my $hi = (substr($high, 0, 1, '') eq ']'); my $l = $self->fmt($low); my $h = $self->fmt($high); $self->{CACHE}{$l} = $v if $li; $self->{CACHE}{$h} = $v if $hi; push @{ $self->{RANGE} }, { LO => $l, HI => $h, LO_INC => $li, HI_INC => $hi, VALUE => $v, CODE => ($l == $h && sub { $l == $_[0] }) || ($li && $hi && sub { ($l ne '' && $l <= $_[0]) and ($h ne '' & +& $_[0] <= $h) } ) || ($li && sub { ($l ne '' && $l <= $_[0]) and ($h ne '' && $_[0] + < $h) } ) || ($hi && sub { ($l ne '' && $l < $_[0]) and ($h ne '' && $_[0] +<= $h) } ) || sub { ($l ne '' && $l < $_[0]) and ($h ne '' && $_[0] < $h) } }; } return $self; } sub range { my ($self, $value) = @_; $value = $self->fmt($value); return $self->{CACHE}{$value} if exists $self->{CACHE}{$value}; $_->{CODE}->($value) and return($self->{CACHE}{$value} = $_->{VALUE} +) for @{ $self->{RANGE} }; return; } package Set::Range::Date; @ISA = qw( Set::Range ); sub fmt { my $d = pop; return sprintf "%04d%02d%02d", ($d =~ /(\d{1,2})\D*(\d{1,2})\D*(\d+)/)[2,1,0]; } package Set::Range::Date::Eu; @ISA = qw( Set::Range ); sub fmt { my $d = pop; return sprintf "%04d%02d%02d", ($d =~ /(\d{1,2})\D*(\d{1,2})\D*(\d+)/)[2,0,1]; } package Set::Range::Time; @ISA = qw( Set::Range ); sub fmt { my $t = pop; # assume it's a timestamp return $t unless $t =~ /\D/; # does it look a like 12-hour clock? if ($t =~ /([apAP])/) { my $is_pm = (lc($1) ne 'a'); my @parts = $t =~ /(\d+)/g; $parts[0] %= 12; $parts[0] += 12 if $is_pm; return sprintf '%02d' x 4, @parts; } # otherwise, assume 24-hour clock return sprintf '%02d' x 4, $t =~ /(\d+)/g; } package Set::Range::Number; @ISA = qw( Set::Range ); sub fmt { pop } 1; __END__
Sample use is:
use Set::Range; my $r = Set::Range::Date->new( '[1/1/2001,12/31/2001]' => "", '[,1/1/2001)' => "before ", '(12/31/2001,]' => "after ", ); print "Today is ", $r->range('5/6/2000'), "now";
Mine uses standard set notation (a bracket means inclusive, and a parenthesis means exclusive) as the key, and some value (such as a code reference) for the value. The sets should be sent in a meaningful order -- the order they'll be tested in.

I also use OO design to specify the formatting as a sub-class action, and leave the actual comparing up to the main class.

This isn't meant to be a challenge or whatever to your module, it's just how I would write the thing. Yours looks rather nice (save that eval(), which you tell me you've gotten rid of in your CPAN'd version).

japhy -- Perl and Regex Hacker

Replies are listed 'Best First'.
Re: Re: Set::Range - conditional testing on sets
by $code or die (Deacon) on Apr 30, 2001 at 04:35 UTC
    This isn't meant to be a challenge or whatever to your module, it's just how I would write the thing. Yours looks rather nice (save that eval(), which you tell me you've gotten rid of in your CPAN'd version).

    Absolutely no offense taken - I'm not into the whole ego thing, I think this is the whole point of open source: It's really nice to see how this can be developed into a useful module that people can use. Coming from a mainly Windows background this is a new experience for me - I like it. Anyway, I did ask for comments\revisions etc.

    Simon
    $code or die
    $ perldoc perldoc

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://76531]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-19 15:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found