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;