Re: [PHP] Adding up values

2002-05-10 Thread Miguel Cruz

There's really not much to it. The intval of '-' is 0, so it doesn't cause 
any problems.

  $numbers = file('path/logfile.txt');
  $total = 0;
  foreach ($numbers as $num) $total += intval($num);

miguel

On Fri, 10 May 2002, Liam MacKenzie wrote:
 another real basic question, how would I add up all the numbers in a log
 file in this format?
 
 Start log file
 
 
 6353
 3309
 294
 762
 6443
 6353
 3309
 -
 -
 -
 -
 -
 -
 294
 762
 6353
 6443
 3309
 -
 -
 -
 -
 -
 -
 -
 -
 294
 867
 859
 7695
 97
 49
 -
 -
 -
 -
 
 ---
 end log file
 
 
 how can I add all the numbers without having those dashes causing a problem?
 
 cheers,
 Liam
 
 
 
 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Adding up values

2002-05-10 Thread Jason Wong

On Friday 10 May 2002 14:27, Miguel Cruz wrote:
 There's really not much to it. The intval of '-' is 0, so it doesn't cause
 any problems.

   $numbers = file('path/logfile.txt');
   $total = 0;
   foreach ($numbers as $num) $total += intval($num);

Or use array_sum():

  $numbers = file('path/logfile.txt');
  $total = array_sum($numbers);

-- 
Jason Wong - Gremlins Associates - www.gremlins.com.hk
Open Source Software Systems Integrators
* Web Design  Hosting * Internet  Intranet Applications Development *

/*
What if is a trademark of Hewlett Packard, so stop using it in your
sentences without permission, or risk being sued.
*/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP] Adding up values

2002-05-09 Thread Liam MacKenzie

Hey all,

another real basic question, how would I add up all the numbers in a log
file in this format?

Start log file


6353
3309
294
762
6443
6353
3309
-
-
-
-
-
-
294
762
6353
6443
3309
-
-
-
-
-
-
-
-
294
867
859
7695
97
49
-
-
-
-

---
end log file


how can I add all the numbers without having those dashes causing a problem?

cheers,
Liam



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php