P is for Practical | |
PerlMonks |
Re: Adding scalars but with a twist..by TilRMan (Friar) |
on Feb 13, 2005 at 18:38 UTC ( [id://430608]=note: print w/replies, xml ) | Need Help?? |
If you want to go a step farther than eval, you can use some tie magic to "shadow" the original $Sel1Ttl variables with a real array. You have to be careful about how you index into this array -- $Sel1Ttl counts up from one, but (modern) Perl arrays count up from zero. In this code, I put a dummy element at index zero so that $a1z = $az[1].
Since the dummy element zero is not a reference, the tied array will croak if you accidentally use it:
With the array, you can use the traditional for (1 .. 3) { $sum += $az[$_] } or the IMHO prettier:
If you'd prefer $Sel1Ttl = $SelTtl[0], leave out the dummy element in the tie. If you do, the sum becomes:
In Section
Seekers of Perl Wisdom
|
|