why doesn't below program produce number of keys?
#!/usr/bin/perl -w use strict; use diagnostics; my $hash = { key1 => 'value1', key2 => 'value2', key3 => 'value3', key1 => 'value3', key1 => 'value33', key1 => 'value3', key2 => 'value3', key2 => 'value3', key4 => 'value3', key4 => '23232', }; #foreach (keys %{$hash}) { # print "$_ => ${$hash}{$_}\n"; #} my %hist; foreach (keys %{$hash}) { $hist{$_}++ for $_; } foreach (keys %hist) { print "$_ , $hist{$_}\n"; } print "size of hash: " , keys(%{$hash}) . "\n"; ././././perl.hash.basic.2 key2 , 1 key1 , 1 key4 , 1 key3 , 1 size of hash: 4
I am looking to find out how many key2 are in the hash and how many key1 is in the hash and etc.. Below tutorial does not tell you how many of each key exists in the hash
Now there are two passes over the list, and the situation isn't going +to get any prettier from here. What you want is basically a histogram + of the data, and you can get that with a hash: my %histogram; $histogram{$_}++ for @list; This hash associates each individual item with its count, and it only +traverses the list once. In a recent case, I was looking at a list of tags associated with vari +ous photographs. To lay out the data for display, it was useful to kn +ow how many different tags there are in the list. I could get that si +mply from the number of keys in the histogram: $unique = keys %histogram; I could also delete the duplicates and end up with a list of the uniqu +e tags: @unique = keys %histogram;

In reply to Re^4: perl sort issue while going through article at perl.com by convenientstore
in thread perl sort issue while going through article at perl.com by convenientstore

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.