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

Re: Set Notation Handling

by kvale (Monsignor)
on Mar 28, 2005 at 19:18 UTC ( [id://442919]=note: print w/replies, xml ) Need Help??


in reply to Set Notation Handling

If you are looking for a module to maniptulate sets, try Set::Scalar:
use Set::Scalar; $s = Set::Scalar->new; $s->insert('a', 'b'); $s->delete('b'); $t = Set::Scalar->new('x', 'y', $z); $u = $s->union($t); $i = $s->intersection($t); $d = $s->difference($t); $e = $s->symmetric_difference($t); $v = $s->unique($t); $c = $s->complement;

From your other post, however, it seems like you are interested in a more specialized form of set, that is integer ranges or spans. In this case, check out the module Set::IntSpan:

use Set::IntSpan qw(grep_set map_set); $set_spec = "1-20000"; $set = new Set::IntSpan $set_spec; $set_spec = "1-14192,14194,14196-14221"; $u_set = union $set $set_spec; $i_set = intersect $set $set_spec; $x_set = xor $set $set_spec; $d_set = diff $set $set_spec; $c_set = complement $set;
For large integral ranges, Set::IntSpan can be a lot more efficient than Set::Scalar.

-Mark

Replies are listed 'Best First'.
Re^2: Set Notation Handling
by tradez (Pilgrim) on Mar 28, 2005 at 22:28 UTC
    This is really a great module, but what would be the simplest way to just say do this:
    $set_spec = '1-4,6,9,12-18'; if ($value IS IN $set_spec){ print "$value is valid\n"; }else{ print "$value is invalid\n"; }


    Tradez
    "Every official that come in
    Cripples us leaves us maimed
    Silent and tamed
    And with our flesh and bones
    He builds his homes"

    - Zach de la Rocha
      Member testing is included in Set::IntSpan. Try this.
      use strict; use Set::IntSpan; + my $set = new Set::IntSpan('1-4,6,9,12-18'); testvalue(3, $set); testvalue(7, $set); + sub testvalue { my $value = shift; my $set = shift; if (member $set $value){ print "$value is valid\n"; }else{ print "$value is invalid\n"; } }
        That works perfectly! Thank you sooo much for your help!


        Tradez
        "Every official that come in
        Cripples us leaves us maimed
        Silent and tamed
        And with our flesh and bones
        He builds his homes"

        - Zach de la Rocha

Log In?
Username:
Password:

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

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

    No recent polls found