in reply to A better way than ||
Also note that, in general, $x = $x || 0; can be stated as $x ||= 0; Another way:my $sum = 0; $sum += ($weights[$_]||0) * ($digits[$_]||0) for 0 .. @#weights;
my $sum = 0; for (0..@#weights) { next unless $weights[$_] && $digits[$_]; $sum += $weights[$_] * $digits[$_]; } # or for (0..@#weights) { $sum += $weights[$_] * $digits[$_] if $weights[$_] && $digits[$_ +]; } # or yet another: $sum += $weights[$_] * $digits[$_] for grep $weights[$_] && $digits +[$_], 0..@#weights;
|
|---|