Link to home
Start Free TrialLog in
Avatar of Tolgar
Tolgar

asked on

How to subtract a number from all elements of an array in Perl?

How can I subtract 2 from all elements of an array without writing an explicit foreach loop?
ASKER CERTIFIED SOLUTION
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oops.  Thinko.  Mine should obviously be -= and - (instead of += and +).  I hate the limited edit time on EE.
When you say "without writing an explicit foreach loop", I'm not sure if you'd prefer an implicit foreach loop, or an explicit map loop, or to invoke code that someone else has already written, but you could also
use Operator::Util qw(hyper);
hyper('-=', \@arr, 2, dwim_right=>1) ;