hash counter

L

Lee

Hey I have a hash table with many many keys. In each entry, the value
is a counter and so my code goes something like:

my %hash=();
while (statement){

$key="blabla";
if (!$hash{$key}){
$hash{$key}=1;
}
elsif ($hash{$key}){
$hash{$key}++;
}
}

How would I speed this up? Someone told me that using the following
code instead would work, but it hasn't.

my %hash=();
while (statement){
$key="blabla";
$hash{$key}++;
}
 
A

A. Sinan Unur

Hey I have a hash table with many many keys. In each entry, the value
is a counter and so my code goes something like:

Please post the smallest *real* program that demonstrates your problem.

Please read the posting guidelines for this group to learn how you can help
others help you.

Sinan.
 
L

Lee

Are you kidding me? I thought that was good pseudocode that anyone
[who could help me] would be able to understand. I'll come up with
something later that would be short and readable if no one can help me
based on that. Promise.

Anyway, any answers in the meantime would be greatly appreciated. And
I will send you flowers.
 
J

jl_post

Lee said:
Are you kidding me? I thought that was good pseudocode that
anyone [who could help me] would be able to understand.

It's not that we don't understand your pseudocode, Lee -- it's just
that we can't figure out why the code that was given to you doesn't
work.

In other words, the code that was give to you looks all right to me.
There doesn't seem to be anything wrong with it, so the problem
probably lies somewhere else. But all you said was: "...using the
following code instead would work, but it hasn't." That doesn't help
us figure out your problem, since everything looks all right and we
aren't given enough information to faithfully reproduce your problem.

It would help us if you could give us the exact code you wrote and the
exact error message you were given. But giving us pseudocode and
saying something like "it doesn't work" doesn't always allow us to
pinpoint the problem in question.
 
L

Lee

okay then I'd like to rephrase my question:

Is it legal to do this without initializing that one entry?
$hash{$key}++;

There are no errors associated with it but it never adds. I'll come up
with real code for you later. Thanks.
 
S

Sherm Pendley

Lee said:
okay then I'd like to rephrase my question:

Is it legal to do this without initializing that one entry?
$hash{$key}++;

Yes, it is. The entry associated with $key will be created as needed.

A simple one-liner can verify that:

sherm$ perl -e '$hash{"foo"}++; print $hash{"foo"}, "\n"'
1
sherm$

sherm--
 
S

Scott Bryce

Lee said:
Is it legal to do this without initializing that one entry?
$hash{$key}++;

There are no errors associated with it but it never adds. I'll come up
with real code for you later. Thanks.

use strict;
use warnings;

my %hash;
my $key = 'key';

$hash{$key}++;

print $hash{$key};



This prints 1 as expected. You will have to show us real code.
 
J

jl_post

Lee said:
Is it legal to do this without initializing that one entry?
$hash{$key}++;

Yes, even when "use strict;" and "use warnings;" is in use.
There are no errors associated with it but it never adds.
I'll come up with real code for you later. Thanks.

That's strange, but the real error probably lies elsewhere. A
short, sample program would help us a lot in finding your error.
 
L

Lee

yeah I totally had a problem with my print statement. I was using an
array of keys before I tried switching over. When I switched over
without the array, I was unable to print any entry of the hash table
because I had no keys. Stupid me.
 
A

A. Sinan Unur

Stupid me.

Some might call this an astute observation after your rant. Instead, I
will again try to direct your attention to the posting guidelines. The
guidelines contain valuable information on how to help other people help
you instead of yelling at them when they ask you to help them help you.

Sinan.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top