in reply to Using += in array context
#!/opt/perl/bin/perl -w use strict; use Tie::Hash; package Hash::Add; @Hash::Add::ISA = qw /Tie::StdHash/; use overload '+' => sub { my ($self, $other) = @_; foreach my $key (keys %$self) { $self -> {$key} += $other -> {$key}; } }; package main; my $totals = tie my %totals => 'Hash::Add'; my $delta = tie my %delta => 'Hash::Add'; my @fields = qw /foo bar/; @totals {@fields} = (10, 20); @delta {@fields} = (5, 6); $totals += $delta; print "Foo = $totals{foo}\n"; print "Bar = $totals{bar}\n"; __END__ Foo = 15 Bar = 26
-- Abigail
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Using += in array context
by bikeNomad (Priest) on Jun 05, 2001 at 21:12 UTC |