in reply to Count array element in HASH of ARRAYS
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" }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Count array element in HASH of ARRAYS
by AnomalousMonk (Archbishop) on Jul 20, 2015 at 14:05 UTC | |
by halfcountplus (Hermit) on Jul 20, 2015 at 14:42 UTC | |
by stm (Initiate) on Jul 20, 2015 at 14:23 UTC | |
|
Re^2: Count array element in HASH of ARRAYS
by stm (Initiate) on Jul 20, 2015 at 14:06 UTC | |
by Laurent_R (Canon) on Jul 20, 2015 at 16:40 UTC | |
by stm (Initiate) on Jul 20, 2015 at 17:46 UTC |