Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A newbie question on hashes

Status
Not open for further replies.

Parke

Programmer
Jan 10, 2001
51
0
0
US
While reading a log file, I am selecting an ID using reg exp and inserting the ID into a hash as the key and setting the value = 1. The nice thing about using a hash is that it filters out the duplicate ID values. If I wanted to be able to count the number of times each ID occurs, how can I tell if the key previously existed in the hash? If I was doing this in vbscript, I could use a dictionary, an error is produced on attempting to enter a duplicate key and I increment the value. Is this possible in Perl? I have tried
if ($table1{$1} gt '0')
{
$table1{$1} = 2;
}
else
{
$table1{$1} = 1;
}

and all I get is 1 as the value.

Thanks for any and all help,
 
I'm not sure I fully understand your question. One way of keeping a count of key occurences is:
Code:
@words = qw( to be or not to be that is the question );

# get number of occurences
map { $occurs{$_}++ } @words;

foreach( sort keys %occurs ) {
  print "$_ occurs $occurs{$_} times\n";
}
Cheers, Neil :)
 
Parke,

Neil(toolkit) is absolutely correct.

And by the way, you were doing a string comparison on numbers, you should have written:
Code:
if ($table1{$1} > 0){ #blah blah blah }
else{ #blah blh bl }
But that method is worthless since it is limited to a maximum of two occurences of a line - just use Neil's method which tracks however many occurences there are.

--jim
 
Neil, Coderifous:

Each line of the log file will look along the line of

"182.170.34.202", "b9245acd231234as" and lots of additional junk

and there might be 30,000 lines/IDs to extract.

The second set of quotes is the ID that I am extracting from each line of the log. When I insert the ID into a hash table as the key, all duplicates are removed which is wonderful but I wonder if it is easy to count the number of times I attempt to add a duplicate key?

Thanks,
 
You must be missing the functionality in the what seems to be a simple peice of code.

In Neil's example, The value that a key points to is incremented every single time it occurs. So if the ID:
"R2D2-C3PO" occurred 47 times in your document, then
$occurs{'R2D2-C3PO'} would point to the value: 47

This allows you to easily detect how many times any given string occured in your document.

Am I missing something?

--jim

 
Jim:

Only that I am very new to Perl and learning. Thanks for pointing out that Neil's example does answer the question.

Again thanks for the help,

Parke
 
I apologize if I came across rudely.

I may take for granted that I've already gone through the 'hard' part of learning perl, and that there are others just beginning the journey.

Hope to see more questions from you, this forum is a good place to learn.

--jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top