Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Returning a Junction

by fletcher_the_dog (Friar)
on Sep 04, 2003 at 19:00 UTC ( [id://288975]=note: print w/replies, xml ) Need Help??


in reply to Returning a Junction

You could make a junction class now using operator overloading:
use strict; package junction; use overload 'eq'=>\&compare; sub new{ my $class = shift; my $self = {}; %{$self} = map{$_,1} @_; bless $self,$class; } sub compare{ my $self = shift; my $string = shift; return exists $self->{$string}; } package main; my $foo = junction->new("Visa Card","Visa","Visa Check Card"); foreach my $test ("Visa","Vasdfasd","Taco","Visa Check Card") { if ($test eq $foo) { print "$test is a Visa!\n"; } else { print "$test is NOT a Visa\n"; } } __OUTPUT__ Visa is a Visa! Vasdfasd is NOT a Visa Taco is NOT a Visa Visa Check Card is a Visa!

Replies are listed 'Best First'.
Re: Re: Returning a Junction
by hardburn (Abbot) on Sep 04, 2003 at 19:20 UTC

    ++fletcher_the_dog

    Only problem is that, while my example above used the any() style junction, there is also the all() type (i.e., all elements in the junction must match).

    Probably the best way around this is to split into two packages: junction_any and junction_all. The implementation for compare under junction_all would be something like this:

    sub compare { my $self = shift; my $string = shift; foreach my $key (keys %{ $self }) { return 0 if $string ne $key; } return 1; }

    For completeness, you'll want to provide equivilent overloading for the other comparison operators, too.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

      I don't know a lot about junctions, but it seems to me in order for a string to ever pass an all_junction compare there could only be one key in the junction. i.e. "Visa" only matches "Visa" in a string compare, if there is anything else the compare will fail. An all junction I would think would only work if you were comparing two junctions.
        And, in fact, it is the logical default for comparing junctions. It's basic set theory. Two sets are equivalent if they contain the same elements. What junctions do need is to see if one junction is a subset of another junction. This is equivalent to asking if every $foo in junction $bar is within the other junction $baz.

        ------
        We are the carpenters and bricklayers of the Information Age.

        The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

        Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-03-28 13:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found