perl - count the number of keys that map to a value -


i have following hash

my %hash = (     w1 => '0',     e2 => '1',     r1 => '2',     o3 => '1',     h4 => '0',     t5 => '1', ); 

i number of keys map each value in hash.

3 keys map value 1. 1 key map value 2. 2 keys map value 0. 

i without using function module.

one way thought of doing looping through values of hash. if value has key increment counter key. problem knowing values beforehand can initialize counters each value. there's easier solution this. think regex might work.

attempt 1

my $string; foreach $value (value %hash) { $string = join(",", $value); }  # count number of occurrences each value (separated commas) 

use hash count occurrences.

#!/usr/bin/perl use warnings; use strict;  %hash = (     w1 => '0',     e2 => '1',     r1 => '2',     o3 => '1',     h4 => '0',     t5 => '1', );  %count; $value (values %hash) {     $count{$value}++; }  $value (keys %count) {     $count = $count{$value};      print "$count key", $count == 1 ? q() : 's',           " map",       $count == 1 ? 's' : q(),           " $value.\n"; } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

javascript - addthis share facebook and google+ url -

ios - Show keyboard with UITextField in the input accessory view -