BrowserUk has asked for the wisdom of the Perl Monks concerning the following question:
You have a (compound) variable that you want to increment and then modulate (ie. bound with modulus some number):
@a = ( 1 .. 10 );; $a[$_] = ( $a[$_]+1 ) % 10 for 0 .. 9;; print @a;; 2 3 4 5 6 7 8 9 0 1
Instinct wants ++$a[$_] %= 10 for 0 .. 9; to work. It doesn't.
Of course there is $a[$_] = ++$a[$_] % 10 for 0 .. 9;; which works, but is hardly better, especially when $a[$_] might be more like:
.$things{ $thing }{$someotherkey}[$someindex]{$somekey}[ $_ ]
A better way?
|
|---|