----- Original Message ----- From: "Hawk" <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Thursday, March 09, 2006 11:26 PM
Subject: comparing contents of two files




Hi,

I have two username/password files from two different systems.  I need the
script to output users that exist in both files to >>match.txt  users that
do not match, output to >>no_match.txt

Users1.txt: (tab delimited)

Abc 123
Scsadffa adjeka*
Etc...


Users2.txt: (tab delimited)

Abc 123
23daas kjiaoa
Etc...

Read file Users1.txt line by line and create hash

my %users1={};
my %users2={};
open(FILE,"<Users1.txt");
while (my $row = <FILE>) {
   $row =~ s/\n//;
   my ($key, $value) = split(/\s/,$row);
   $key =~s/\s//g; # delete all whitespace characters
   $value =~s/\s//g;
   $users1->{$key} = $value;
   }
close FILE;

Now read file Users2.tx to other hash

open(FILE,"<Users2.txt");
while (my $row = <FILE>) {
   $row =~ s/\n//;
   my ($key, $value) = split(/\s/,$row);
   $key =~s/\s//g; # delete all whitespace characters
   $value =~s/\s//g;
   $users2->{$key} = $value;
   }
close FILE;

Now compare all keys from %users1 with keys in %users2

foreach my $key(keys %users1) {
   if(defined $users2->{$key} and $users2->{$key} eq $users1->{$key})  {
           # keys exist in both hashes and values are identical
           # write to file what you want
           }
   elsif(defined $users2->{$key} and $users2->{$key} ne $users1->{$key})  {
           # keys exist in both hashes but values are different
           # write to other file what you want
           }
   else {
           # key from hash 1 not exist in hash 2
           # write some other to ...
           }
}

Petr Vileta, Czech republic
(My server reject all messages from Yahoo and Hotmail. Send me your mail from another non-spammer site please.)


_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to