my $newno = 0; while (my ($number) = $sth->fetchrow_array) { $numbergraf++; # whatever that is $newno += $number; } $output .= qq~ The sum of xxx is $newno ~;