We help IT Professionals succeed at work.

Perl : How to check if a value already exists in a hash

ambuli
ambuli asked
on
530 Views
Last Modified: 2012-05-11
I am storing some data in a hash.

For example,
key1, value11, value12, value13
key2, value21, value22, value23

However, sometime I have to store data that has the same key value as an existing one, but with a different value.

key1, value11, value12, value13
key2, value21, value22, value23
key1, value31, value32, value33   <--- same key, but different values

Can this be stored?  If so, I need a way to check if the data vales are different.

if ( exists $Hash{key1} )
{
    // Now I know that ke1 exists.  but how can I check if the values are also the same?

}
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2007

Commented:
I would suggest using a different data structure.  Possibly a hash of hashes.

Can you explain a bit more what you are doing with the data?
CERTIFIED EXPERT
Top Expert 2007

Commented:
Thinking about it again, a hash of arrays is probably better suited to your needs.
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Hi there,
sorry for the delay.  Here is what I am trying to do.  I have included the script and the log file that I wanted to process.  The issue is some of the stack traces have "|No Detail message" as the first line, and this is what I am using as the key.  So, my results end up wrong.
I want to summarize how many of each different exceptions with different stack trace found in the log file.

Thanks for all the help guys.
#!/usr/bin/perl -w

$infile = "data.txt";
open (MYFILE, '>summary.txt');

open(DATA, $infile);

%HashOfExceptions = ();
%HashOfRepetitions = ();

@lines_var = <DATA>;

for( $i =0; $i < $#lines_var; $i++ )
  {
	if($lines_var[$i] =~ /Java Exception/ )
	  {
		if ($lines_var[$i] =~ /^..\d/)
		  {
			$lines_var[$i] = substr($lines_var[$i], 17);
		  }
		if ( exists $HashOfExceptions{$lines_var[$i+1]} )
		  {
			$HashOfRepetitions{$lines_var[$i+1]} = 	$HashOfRepetitions{$lines_var[$i+1]} + 1;
		  }
		else
		  {
			
			$HashOfRepetitions{$lines_var[$i+1]} = 1;
			
			&storeUnique($lines_var[$i+1], $lines_var[$i], $lines_var[$i+2], $lines_var[$i+3]);
		  }		
	  }
	
  }
&printHash();
close(DATA);

sub storeUnique
  {
	my($inkey, $invalue1, $invalue2, $invalue3) = @_;
	
	push @ { $HashOfExceptions{$inkey}} , $invalue1,"\t", $inkey, "\t", $invalue2, "\t", $invalue3;
  };
  
sub printHash
  {
	for $test (keys %HashOfExceptions )
	  {	
		chomp($HashOfExceptions{$test});
		print MYFILE "\t @{ $HashOfExceptions{$test}}  \t\t$HashOfRepetitions{$test}\n";
	  }
  };





The data.txt file looks like below:

2011042510331546	 Java Exception - IllegalArgumentException
 	 | Entity[] is invalid
 	 | net_cldc_lib-3(4DB3A352)
 	 |  AppManagerImpl
  		1
	 Java Exception - IllegalArgumentException
 	 | No detail message
 	 | net_cldc-22(4DB39CD1)
 	 |  ObjectArraySort
  		2

2011042510341813	 Java Exception - IllegalStateException
 	 | EGLManager::CreateWindowSurface failed
 	 | net_cldc-32(4DB39CD1)
 	 |  GraphicsSurfaceImpl
  		1
	 Java Exception - IOException
 	 | APN is not specified.
 	 | net_os-3(4DB3A18C)
 	 |  ClientProtocol
  		10
	 Java Exception - IllegalArgumentException
 	 | Entity[] is invalid
 	 | net_search_lib-3(4DB3A352)
 	 |  AppContentManagerImpl
  		1
	 Java Exception - NullPointerException
 	 | No detail message
 	 | net_test_app-1(4DB3A355)
 	 |  HomeManager
  		60

Open in new window

ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
Maybe the intent of your code would be easier to follow if you would show us what result you would want to generate when given that input.
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
You say my results end up wrong, but you do not say what the correct results should be, so I'm not sure what corrections would need to be made to achieve what you want.

Author

Commented:
Hi Ozo:

Thanks for the reply.  I did solve my problem by modifying my key value.  I was having problem when I have identical keys, but different values.  I modified my code so that by concatenating two values, I got a unique key.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.