Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

How do I extract the number of times a value appears in a hash?

( #30630=categorized question: print w/ replies, xml ) Need Help??
Contributed by v on Sep 01, 2000 at 00:39 UTC
Q&A  > hashes


Description:

I'm creating a hash that contains IP_Address:Size objects. The same IP_Address occurs many times, as is Size. i want to know the following:
  • how many times each IP_Address appears
  • sum of Size for any given IP_Address

Answer: How do I extract the number of times a value appears in a hash?
contributed by BlaisePascal

Is the "IP_Address:size" blocks the hash values, or the hash keys?

Assuming the the IP_Address:size blocks are the hash values (say, you were keying off of a connection ID, or something), then you could do something like:

my %counts; my %sizes; for (values %inhash) { my ($ip, $size) = split /:/; next unless defined $ip; $counts{$ip}++; $sizes($ip)+= $size; } for $ip (keys $counts) { my ($count, $size) = ($counts{$ip},$sizes{$ip}; print "IP Address $ip appeared $count times, for a total size of $si +ze\n"; }
Does that help?
Answer: How do I extract the number of times a value appears in a hash?
contributed by ton

Assuming that "IPaddr:Size" are the values:

map {if /^(.*):(\d*)$/ {$count{$1}++;$size{$1}+=$2;}} values(%hash);
This will place results in the %count and %size hashes, keyed by IP address. If "IPaddr:Size" are the keys, just replace the 'values' with 'keys' in the fragment above.
Answer: How do I extract the number of times a value appears in a hash?
contributed by dsb

Pardon the building of the hash

#! /usr/bin/perl %hash = ( '1' => 'foo', '2' => 'bar', '3' => 'foo', '4' => 'foo', ); %count; foreach $key ( keys %hash ) { $val = $hash{$key}; $count{$val}++; } foreach $key1 ( keys %count ) { print $key1 . " appears " . $count{$key1} . " times\n"; }

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (5)
    As of 2015-07-05 06:27 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (60 votes), past polls