#!/usr/bin/perl use strict; package ary; use Math::Matrix; @ary::ISA = qw(Math::Matrix); use overload '+' => \&aryadd; sub aryadd { return $_[0]->add($_[1]); }; package main; my( %totals, %delta ); my @fields = qw/foo bar bat/; @totals{ @fields } = ( 10, 20, 30 ); @delta{ @fields } = ( 5, 6, 1 ); my $t = new ary([@totals{ @fields }]); my $d = new ary([@delta{ @fields }]); $t += $d; # this works too: # $t = $t->add($d); # $t = $t + $d; $t->print("Totals:\n"); # this works too: # my $aref = $t->[0]; print "> $_\n" foreach (@$aref);
Updated: Spurred on to create this version which implements overloading. If you just use Math::Matrix objects and their add method as-is, you don't need an extra package but it's less maintainable and less fun. :)
Updated again: Added last two lines to pull data out of Matrix object.
In reply to Re: Using += in array context
by mattr
in thread Using += in array context
by grinder
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |