Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: comparing two lists

by scottstef (Curate)
on Jun 24, 2001 at 19:21 UTC ( #91069=note: print w/replies, xml ) Need Help??


in reply to comparing two lists

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://91069]
help
Chatterbox?
[Tanktalus]: Discipulus: I should be working on an interview test right now. Well, I kind of am, but keep getting myself distracted :)
[Lady_Aleena]: marioroy, nope, that IS the error.
[Discipulus]: oh to be distracted PM is still a wonderfull place
[Tanktalus]: Problem is, it's a test for another monk - as in, another monk letting me interview for his company :)
[Tanktalus]: He might see me here... :D
[Discipulus]: it's me? ;=)
[marioroy]: Lady_Aleena could it be a quoting issue? I just tried find with -exec on the command line. find . -name "*.pl" -exec ls -l {} \;
[Tanktalus]: No, I know who he is, nice try. But if you're hiring, let me know :)
[Tanktalus]: Lady_Aleena: yeah, I can reproduce your issue, it's confusing alright :)
[Tanktalus]: I'm sure it'll make perfect sense once we figure it out :)

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2017-04-23 20:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (432 votes). Check out past polls.