This retains the keys in the `%containers` hash, if you don't need to do that you could replace the loops with:#!/usr/bin/perl -w use strict; my %HoA = ( key1 => [ "50001", "01" ], key2 => [ "50011", "02" ], key3 => [ "50010", "01" ], key4 => [ "50041", "02" ], key5 => [ "50301", "02" ], key6 => [ "50701", "09" ], key7 => [ "50801", "09" ] ); my %containers; while (my ($k, $v) = each (%HoA)) { push @{$containers{$v->[0]}}, $k; } foreach (sort keys %containers) { print "Container $_ has ".@{$containers{$_}}." keys.\n" }
my %containers; while (my ($k, $v) = each (%HoA)) { $containers{$v->[1]}++; } foreach (sort keys %containers) { print "Container $_ has $containers{$_} keys.\n" }
In reply to Re: Count array element in HASH of ARRAYS
by halfcountplus
in thread Count array element in HASH of ARRAYS
by stm
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |