Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Pair of items

by dragonchild (Archbishop)
on Jul 29, 2003 at 20:07 UTC ( [id://278959]=note: print w/replies, xml ) Need Help??


in reply to Pair of items

To compare any two groups, you'd do something like:
sub is_ok { my ($x, $y) = @_; my (%x, %y); @x{@$x} = 1; @y{@$y} = 1; foreach my $k (keys %x) { return 0 if exists $y{$k}; } return 1; }
Put in a loop, as necessary. Lather, rinse, repeat. A few notes:
  1. This will work with groups of arbitrary size and will also work with groups of different sizes.
  2. It will also work with groups that contain arbitrary data.
  3. It will throw warnings if any of the elements are undef.
  4. It does string comparisons, so 2, 4, 6 and 1.0, 2.0, 3.0 may or may not be ok. I will leave numeric comparisons as an exercise for the reader.

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

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

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

Replies are listed 'Best First'.
Re: Pair of items
by tadman (Prior) on Jul 29, 2003 at 20:14 UTC
    Tweakism to get something like this:
    sub is_ok { my ($x, $y) = @_; my %x; @x{@$x} = 1; return !grep { exists($x{$_}) } @$y; }
    You seem to create a hash and then just iterate over its keys, instead of just using the straight list. Here I just make one hash and use the other list raw.

    grep is used to produce a list of matches, which is then negated, so that it returns a true value if there are no matches.
Re: Re: Pair of items
by Anonymous Monk on Jul 30, 2003 at 16:09 UTC
    Quantum::Superpositions offers an elegant solution:
    use Quantum::Superpositions; my $list1 = [1,2,3]; my $list2 = [4,5,6]; print "No common items" if all(@$list1) != all(@$list2);
    Basically, if all of the elements in list1 are not equal to all of the elements in list2, that will print "No Common Items"

    HTH

    -Tom

      And, that is the solution I would propose in Perl6.

      However, Quantum::Superpositions is not a core module nor is it meant to be used in production code. It's a plaything conceived in the bowels of TheDamian's mind that hasn't been thoroughly tested. *shrugs* TMTOWDI

      ------
      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://278959]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-24 16:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found