?
Solved

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

Posted on 2011-04-26
7
Medium Priority
?
487 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?

}
0
Comment
Question by:ambuli
  • 3
  • 2
  • 2
7 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 35472557
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?
0
 
LVL 48

Expert Comment

by:Tintin
ID: 35472564
Thinking about it again, a hash of arrays is probably better suited to your needs.
0
 
LVL 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 35472738
You can only store one value at a given key, but that value can be an array ref or a hash ref
as:
$Hash{key1} = ["value21, value22, value23", "value31, value32, value33"];

or, if you want an easy way to check if a particular value exists
$Hash{key1} = {"value21, value22, value23" =>1, "value31, value32, value33" => 1};
if(  $Hash{key1}{"value21, value22, value23"} ){
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ambuli
ID: 35476220
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

0
 
LVL 85

Expert Comment

by:ozo
ID: 35479612
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.
0
 
LVL 85

Expert Comment

by:ozo
ID: 35479631
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.
0
 

Author Comment

by:ambuli
ID: 35484086
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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Six Sigma Control Plans
Suggested Courses

571 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