Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Unique value count in hash not working properly

by koolgirl (Hermit)
on Sep 30, 2011 at 12:40 UTC ( #928763=perlquestion: print w/replies, xml ) Need Help??

koolgirl has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

Yesterday, I asked about counting and recording unique elements in a list of lots of doubles. I got a lot of help, and solved problem just fine.

Today, I had a different set of data I needed to do the same thing for, so I used the same exact code. Works as far as counting uniqueness, but the output is messed up

foreach $batch_dates (@dates) { if ($count{$batch_dates}) { $count{$batch_dates}++; #print $count{$batch_dates} . "\n"; } else { $count{$batch_dates} = 1; } # end if } # end foreach foreach $key(keys(%count)) { print $key . " = " . $count{$key} . "\n"; } # end foreach }
Above is the code. In the out put, it first lists the correct info, key(unique id) and value(number of times seen), but then it adds in a line, printing the first key of the list, and then putting the total number of values altogether as the value.

An example

user id1 = 1 user id2 = 1 user id1 = 2

That last line is my problem. I don't understand why the count isn't working...?

koolgirl Nicole update: fixed a typo

"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore all progress depends on the unreasonable man.." -- George Bernard Shaw

Replies are listed 'Best First'.
Re: Unique value count in hash not working properly
by toolic (Bishop) on Sep 30, 2011 at 12:54 UTC
    Please... In other words, post something which anyone can run to duplicate the problem you are having. When I fix the syntax error, create a phony input array and run your code, it seems to do something reasonable.

      I am using strict and warnings. It is somewhat indented correctly, it's a subroutine, and I'm still working on it, so the extra lines will be cleaned up.. I don't see a syntax error, unless you're talking about the extra curly brace at bottom, which is just the closing of the sub, I didn't get the sub name in copy/paste.


      "The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore all progress depends on the unreasonable man.." -- George Bernard Shaw

        I don't see a syntax error, unless you're talking about the [syntax errors].

        So do we have a standalone, runnable code example with sample input and actual and expected output? This is what the monks are asking (begging) for. Please, please help us to help you!

Re: Unique value count in hash not working properly
by keszler (Priest) on Sep 30, 2011 at 13:27 UTC

    Incidentally, your

    if ($count{$batch_dates}) { $count{$batch_dates}++; } else { $count{$batch_dates} = 1; } # end if
    could be replaced by just
    $count{$batch_dates}++;
    If no $batch_dates key previously existed in %count, then the first use of it with ++ will autovivify (with numeric value zero) and increment to 1.

      Assignment, bar key is assigned the value 1
      my %foo; $foo{bar} = 1;

      Autovivification, a hashref springs into existence under key fa , bar key is assigned the value 1

      my %foo; $foo{fa}{bar} = 1;

      Autovivification, you use it as a reference to a hash, , it becomes a reference to a hash

       my $foo; $foo->{bar} = 1;

      Autovivification, you use it as a reference to an array, it becomes a reference to an array

       my $foo; $foo->[0]= 1;

      Initialization, you declare $foo and assign a reference, you can use it as a reference, but it doesn't become a reference, because it already is a reference

       my $foo = {}; $foo = \my %bar; $foo->{fa} = 1;

      Error, can only autovivify if undef, Not an ARRAY reference

       my $foo = {}; $foo->[0] = 1;

      Error, can only autovivify once, Not a HASH reference

       my $foo; $foo->[0] = 'autovivified'; $foo->{autovivified} = 'already';
Re: Unique value count in hash not working properly
by Ratazong (Monsignor) on Sep 30, 2011 at 12:58 UTC

    Your code works for me ...

    please check

    • the array @dates contains exactly what you expect
    • that the hash is empty before you start your counting
    • that the last line of output is not due to some print-statement after your code-fragment (*)
    hth, Rata

    (*) I guess that is the reason for your issue

Re: Unique value count in hash not working properly
by reisinge (Hermit) on Sep 30, 2011 at 12:57 UTC
Re: Unique value count in hash not working properly
by Khen1950fx (Canon) on Oct 01, 2011 at 09:49 UTC
    I haven't done a counter in ages:). Would this help?
    #!/usr/bin/perl -slw use strict; my %count; my(@dates) = ( 9.1.11, 9.2.11, 9.3.11, 9.4.11, 9.5.11 ); foreach my $batch_dates (@dates) { ++$count{$_} foreach (@dates); sleep 1; print $count{$batch_dates}; }
    Update: I added List::MoreUtils for a unique counter:
    #!/usr/bin/perl -slw use strict; use List::MoreUtils qw(uniq); my %count; my (@nums) = uniq 9.2.11, 9.2.11, 9.2.11, 9.4.11, 9.5.11; foreach my $num (@nums) { ++$count{$_} foreach (@nums); sleep 1; print $count{$num}; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://928763]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2022-08-14 09:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?