Solved

finding the differences in 2 hash tables

Posted on 2003-11-04
7
199 Views
Last Modified: 2010-05-18
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 ?

0
Comment
Question by:plennon
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 6

Expert Comment

by:sstouk
Comment Utility
# 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;
0
 
LVL 2

Expert Comment

by:ultimatemike
Comment Utility


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;
0
 
LVL 2

Expert Comment

by:ultimatemike
Comment Utility
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;
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 18

Expert Comment

by:kandura
Comment Utility
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?
0
 

Author Comment

by:plennon
Comment Utility
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!!!!

0
 
LVL 18

Accepted Solution

by:
kandura earned 150 total points
Comment Utility
That does clear up things a bit, yes.
It seems to me that hash1 contains the items in the wrong format: the keys of hash2 are values in hash1.

So if I understand you correctly, you would want the following written out to the file:
L-15, 100
L-25, 55
L-18, 0
L-8, 0

right?

So you do not really need the keys in hash1, only the values.
Now, you could loop over the values in hash1 (that's the elements on the right side of the '=>'),
and setting the value in hash2 to 0 for all the ones that are missing.
After that you print out the contents of hash2 in the format that you want.

# get the missing names
@missing_names = grep { ! exists $hash2{$_} } values %hash1; # note that I use 'values' here

# set their value to 0 in hash2
$hash2{$_} = 0 foreach @missing_names;

# print them out
# first open the file
open F, ">yourfilenamehere" or die $!;

# loop over the hash2
while( my ($k, $v) = each %hash2) {
    print F qq($k, $v), $/;                      # this prints e.g. L-15, 55 on a line
}
close F;

HTH,
Kandura


0
 

Author Comment

by:plennon
Comment Utility
BRILLIANT!!!!


thanks kandura

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
perl split by | 2 80
How to search multiple patterms in a file with perl? 4 77
Awk Question 2 116
combine multiple lines 2 21
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now