lfc_lad Posted June 29, 2009 Share Posted June 29, 2009 Hi guys, I am a sort of newbie, so be nice 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. ??? Link to comment https://forums.phpfreaks.com/topic/164076-help-reading-a-txt-file-and-calculating-the-averages-of-all-the-numbers/ Share on other sites More sharing options...
dzelenika Posted June 29, 2009 Share Posted June 29, 2009 Hi guys, I am a sort of newbie, so be nice 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']++; } Link to comment https://forums.phpfreaks.com/topic/164076-help-reading-a-txt-file-and-calculating-the-averages-of-all-the-numbers/#findComment-865554 Share on other sites More sharing options...
lfc_lad Posted June 29, 2009 Author Share Posted June 29, 2009 Thanks, but i assume where you have Put dots in the code example i would need to put some stuff in it? Sorry for bein a noob lol. I only know the basics. Link to comment https://forums.phpfreaks.com/topic/164076-help-reading-a-txt-file-and-calculating-the-averages-of-all-the-numbers/#findComment-865569 Share on other sites More sharing options...
lfc_lad Posted June 29, 2009 Author Share Posted June 29, 2009 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? Link to comment https://forums.phpfreaks.com/topic/164076-help-reading-a-txt-file-and-calculating-the-averages-of-all-the-numbers/#findComment-865590 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.