in reply to += a hash slice?

For a casual counter this might be overkill, but you could use a closure:

use strict; use warnings; use Data::Dumper; sub make_counter { my ( $tally, @labels ) = @_; return sub { $tally->{$_} += shift for @labels } } my %totals; my $counter = make_counter \%totals, qw(a b c); for(1..10){ $counter->(1,2,3); } print Dumper \%totals;

Replies are listed 'Best First'.
Re^2: += a hash slice?
by Random_Walk (Prior) on Jun 01, 2015 at 15:10 UTC

    I love this solution, but it would scare the horses; I went with the simpler for loop version.

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!