hash counter

Discussion in 'Perl Misc' started by Lee, Feb 17, 2005.

  1. Lee

    Lee Guest

    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}++;
    }
     
    Lee, Feb 17, 2005
    #1
    1. Advertising

  2. "Lee" <> wrote in news:1108678910.569798.145050
    @c13g2000cwb.googlegroups.com:

    > 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.
     
    A. Sinan Unur, Feb 17, 2005
    #2
    1. Advertising

  3. Lee

    Lee Guest

    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.
     
    Lee, Feb 17, 2005
    #3
  4. Lee

    Guest

    Lee wrote:
    >
    > 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.
     
    , Feb 17, 2005
    #4
  5. Lee

    Lee Guest

    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.
     
    Lee, Feb 17, 2005
    #5
  6. Lee wrote:

    > 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--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
     
    Sherm Pendley, Feb 17, 2005
    #6
  7. Lee

    Scott Bryce Guest

    Lee wrote:

    > 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.
     
    Scott Bryce, Feb 17, 2005
    #7
  8. Lee

    Guest

    Lee wrote:
    >
    > 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.
     
    , Feb 17, 2005
    #8
  9. Lee

    Lee Guest

    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.
     
    Lee, Feb 17, 2005
    #9
  10. "Lee" <> wrote in news:1108684522.091414.285580
    @f14g2000cwb.googlegroups.com:

    > 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.
     
    A. Sinan Unur, Feb 18, 2005
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. The Eeediot
    Replies:
    3
    Views:
    2,249
    =?Utf-8?B?UnVsaW4gSG9uZw==?=
    Dec 22, 2004
  2. George2
    Replies:
    1
    Views:
    808
    Alf P. Steinbach
    Jan 31, 2008
  3. rp
    Replies:
    1
    Views:
    543
    red floyd
    Nov 10, 2011
  4. Srijayanth Sridhar
    Replies:
    19
    Views:
    629
    David A. Black
    Jul 2, 2008
  5. John Mr.
    Replies:
    2
    Views:
    96
    Robert Klemme
    Feb 24, 2010
Loading...

Share This Page