Link to home
Start Free TrialLog in
Avatar of plennon
plennon

asked on

finding the differences in 2 hash tables

I have a hash table that contains all possible names (called hashtable1)
I also have another hash table (called hashtable2) created by reading a data file (the data file contains names and values)

the only difference is that hashtable1 contains all possibilities and that hashtable2 only contains certain names and values

I want to display a list of the names that are in hashtable1 that are not in hashtable2 (NOTE: the values in the list from hashtable1 that are not in hashtable2 should be set to zero)

i also want to write to a file all the names and values that are in hashtable2 and the names from hashtable1 that aren't in hashtable2

does this make sense ?

Avatar of Sergy Stouk
Sergy Stouk
Flag of Canada image

# HERE - Complete tested solution:

my(%Hash01) = undef;
my(%Hash02) = undef;

$Hash01{el01} = "01";
$Hash01{el02} = "02";
$Hash01{el03} = "03";
$Hash01{el04} = "04";
$Hash01{el05} = "05";
$Hash01{el06} = "06";

$Hash02{el01} = "01";
$Hash02{el02} = "02";
$Hash02{el06} = "06";

my($key) = undef;
my($OutputHash2) = undef;
my($OutputDifference) = undef;
my($OutFileHash02) = "namesandvaluesofhash02.txt";
my($OutFileHash01) = "notpresentinhash01.txt";

foreach $key (keys %Hash01)
{
      if (defined $key)
      {
            
            if (defined $Hash02{$key})
            {
            # If key name from %Hash01 exists in %Hash02
            $OutputHash2 .= "Name $key, Value $Hash02{$key}\n";
            } else {
                  # If key name from %Hash01 Does Not exists in %Hash02
                  # Set the value of this key name to zero
                  $Hash01{$key} = 0;
                  $OutputDifference .= "$key\n";
                  };
      };
};

      open(OUT,  "> $OutFileHash01") or die $!;  
      print OUT $OutputDifference;
      close OUT;

      open(OUT,  "> $OutFileHash02") or die $!;  
      print OUT $OutputHash2;
      close OUT;
exit;
Avatar of ultimatemike
ultimatemike



Just use the exists keyword to check if each key in hash 1 exists in hash 2 - print out any that aren't.


open LOGFILE, ">mylogfile";

foreach ( keys ( %hashtable1 ) {

   if ( !exists $hashtable{$_} ) {
         print "$_ does not exist in hash2\n";
         print LOGFILE, "$_\n";
   }

}


close LOGFILE;
forgot a ) on the foreach: should be


open LOGFILE, ">mylogfile";

foreach ( keys ( %hashtable1 ) ){

  if ( !exists $hashtable{$_} ) {
        print "$_ does not exist in hash2\n";
        print LOGFILE, "$_\n";
  }

}


close LOGFILE;
How about using grep?

# get the missing names
@missing_names = grep { ! exists $hash2{$_} } keys %hash1;

# display them
print "Missing names: ", join(', ', @missing_names) , $/;

# set values in hash1 to 0
$hash1{$_} = 0 foreach @missing_names;


What I don't really understand is why you would want to write the contents of hash2 to a file, when you got them from a file in the frst place. Could you explain that?
Avatar of plennon

ASKER

The reason i'm writing to a file is because

hash2 is reading info about equipment from a file (the file contains about 20 bits of data per line (i am only reading 2 bits of data  per line, the name and a value)

hash1 contains all the bits of equipment available (only storing the name in hash1)

hash2 stores the name and value in the hash table

all the names and values in hash2 are written to another file with

all the names in hash1 minus the names that are in hash2 (when the names from hash1 are written to file they are given the value zero)

When all info for the equipment is written to a file the information is used to produce a graph

the data is stores as follows


%hash1 = ("001"=>"L-15", "002"=>"L-18", "003"=>"L-25", "004"=>"L-8", );

%hash2 = ("L-15"=>"100", "L-25"=>"55", );


i want to compare the two names

does this make things clearer ???? beacuse i'm confusing myself here!!!!

ASKER CERTIFIED SOLUTION
Avatar of kandura
kandura

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of plennon

ASKER

BRILLIANT!!!!


thanks kandura