Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Need advice on hashes and methods

by injunjoel (Priest)
on Aug 15, 2006 at 18:02 UTC ( #567514=note: print w/ replies, xml ) Need Help??


in reply to Need advice on hashes and methods

Late to the party as usual but here is what I would do.

#!/usr/bin/perl -w use strict; use Data::Dumper; #build some test data here. #create the lists of overlapping values #and unique values my @list_1 = (1 .. 20); my @list_2 = (10 .. 40); #define our sets my (%hash1, %hash2); #we are not interested in the values just #the keys for this exercise so we use a #hash slice, the @hash{@list_of_keys} #construct to build our sample hashes. @hash1{@list_1} = (); @hash2{@list_2} = (); #filter out our stuff. #I like do blocks since they return stuff. my @in1only = do{ #I localize %_ here and set it to %hash1 #you could also use my %temp_hash_for_this_scope #for example but the effect is the same. local %_ = %hash1; #again we see the hash slice thingie this time #we use it with delete which it just so happens #can work with a hash slice. Here we use it to #get rid of keys from %hash2 that might be in #our %hash1. delete @_{keys %hash2}; #finally we return a sorted list of the keys left #over from the delete call above. These are keys #unique to %hash1 only sort keys %_; }; #<---- don't forget the semi-colon for the do block!!!! #same as above... well kinda, just in reverse :) my @in2only = do{ local %_ = %hash2; delete @_{keys %hash1}; sort keys %_; }; print "in one only\n"; print Dumper(\@in1only); print "\n"; print "in two only\n"; print Dumper(\@in2only); print "\n";
and the output...
in one only $VAR1 = [ '1', '2', '3', '4', '5', '6', '7', '8', '9' ]; in two only $VAR1 = [ '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40' ];
-InjunJoel

"I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo


Comment on Re: Need advice on hashes and methods
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2014-12-28 23:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (183 votes), past polls