Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I recently did something similar comparing items that were in two different files using hashes. Each hash had a primary key that was the list name with a value of 1 if it exixted in the first file, 2 if it existed in both files, and 3 if it was only in the secoond file.

my $file1 = "/home/scott/file1"; my $file2 = "/home/scott/file2"; my $hashOfLists; my %hashOfLists; open (FILE1, "$file1") or die "Could not open $file1 $!"; open (FILE2, "$file2") or die "Could not open $file2 $!"; ###################################################### #####This block reads all of the items of the file into #####a hash with the item being the key. Since I ##### was comparing 2 files each key received a #####value of 1 if it was in the first file, 2 if #####it was in both , and 3 if it was only in file 2 ###################################################### foreach (<FILE1>) { chomp $_; $hashOfLists{$_} =1; } foreach (<FILE2>) { chomp $_; ###################################################### #####if exists checks the hash to see if $_ exists. ##### If true $_'s value becomes 2, #####if false, $_ gets a value of three ###################################################### if (exists $hashOfLists{$_}) { ($hashOfLists{$_}) =2; } else { ($hashOfLists{$_})=3; } } close FILE1 or die "Could not close $file1 $!"; close FILE2 or die "Could not close $file2 $!";

"The social dynamics of the net are a direct consequence of the fact that nobody has yet developed a Remote Strangulation Protocol." -- Larry Wall


In reply to Re: comparing two lists by scottstef
in thread comparing two lists by ashaman

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 about the Monastery: (1)
As of 2024-04-19 18:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found