Jump to content

Help! Reading a txt file, And calculating the averages of all the numbers.


lfc_lad

Recommended Posts

Hi guys,

 

I am a sort of newbie, so be nice  :D

 

I have a txt file with some data in it, Which contains numbers and text. The format of the data is as such:

 

20:54 Sun 28/06/09

Arcade - lfc_lad got a break of 55

20:21 Sun 28/06/09

Regular - lfc_lad got a break of 58

 

I managed to search the data for the occurance of a / and delete the line if it occurred. So now the format is

 

Arcade - lfc_lad got a break of 55

Regular - lfc_lad got a break of 58

 

The data could be numerous multiple lines. All i want to do now, Is read all the numbers and calculate the average of these numbers which then gets displayed.  The format will always be:

 

(game) - (player) got a break of (break)

 

I am sure its quite simple, but i am unsure so a small snippet of code on this would be highly appreciated, the txt file is called num.txt.

 

???

 

 

Hi guys,

 

I am a sort of newbie, so be nice  :D

 

I have a txt file with some data in it, Which contains numbers and text. The format of the data is as such:

 

20:54 Sun 28/06/09

Arcade - lfc_lad got a break of 55

20:21 Sun 28/06/09

Regular - lfc_lad got a break of 58

 

I managed to search the data for the occurance of a / and delete the line if it occurred. So now the format is

 

Arcade - lfc_lad got a break of 55

Regular - lfc_lad got a break of 58

 

The data could be numerous multiple lines. All i want to do now, Is read all the numbers and calcul ate the average ofthese numbers which then gets displayed.  The format will always be:

 

(game) - (player) got a break of (break)

 

I am sure its quite simple, but i am unsure so a small snippet of code on this would be highly appreciated, the txt file is called num.txt.

 

???

 

You should play with str_pos() and substr() functions like:

foreach($file as $line)
{ $game =substr($line, stripis($line, '...',) ...);
   $player =substr($line, stripis($line, '...',) ...);
   $break =substr($line, stripis($line, '...',) ...);
   $results[$game][$player]['sum'] += $break;
   $results[$game][$player]['count']++;
}

I done more searching on google... I came across this which, i understand a bit more than the above.

 

<?php
$total = 0;

$fin = @fopen("num.txt", "r");
if ($fin) {

while (!feof($fin)) {
$buffer = fgets($fin);
if (preg_match('/got a break of "(\d+)"/',$buffer,$matched))
$total += $matched['1'];
else
continue;
}
fclose($fin);
print $total;

}

 

I guess that would show the total amount  ??? , if it works. I am currently at work, so unable to test it. If i can get that to show the total of all the numbers after 'got a break of' then i can easily re write it to calculate average.

 

Would the above be a better method for a newbie to use?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.