Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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

In reply to Re: Need advice on hashes and methods by injunjoel
in thread Need advice on hashes and methods by tom2112

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-04-26 06:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found