pmells

msg:4116321 | 1:23 pm on Apr 15, 2010 (gmt 0) |
Not sure if this helps but something like the following will count how many times a number appears... not sure it's the best way to do it and I'm not sure how to isolate it just down to 5 and 1 not sure I'd want to actually as you will have numbers that appear the same amount of times!
<?php
$lines = file('test.txt'); $numsArr = array(); $lastArr = array(); foreach($lines as $line) { $line = trim($line); $nums = explode(" ", $line); $t = count($nums); $n = 0; while($n < ($t-1)) { $num = $nums[$n]; if(!is_array($numsArr[$num])) { $numsArr[$num] = array(); } $numsArr[$num][] = $num; $n++; } $last = $nums[$t-1]; if(!is_array($lastArr[$last])) { $lastArr[$last] = array(); } $lastArr[$last][] = $last; } echo "Normal Numbers:" . "<br />\n"; for($n = 0;$n <= 99; $n++) { foreach($numsArr as $key => $value) { if($key == $n) { echo "<span style=\"display: inline-block; font-weight: bold; width: 35px;\">" . $key . ":</span> " . count($value) . "<br />\n"; } } } echo "Last Numbers:" . "<br />\n"; for($l = 0;$l <= 99; $l++) { foreach($lastArr as $key => $value) { if($key == $l) { echo "<span style=\"display: inline-block; font-weight: bold; width: 35px;\">" . $key . ":</span> " . count($value) . "<br />\n"; } } } ?>
This does rely on the data always arriving in that form as well each set of numbers on a new line and separated by a space! Just change the name of the file from test.txt to whatever you want!
|
nimonogi

msg:4116338 | 2:25 pm on Apr 15, 2010 (gmt 0) |
Thanks for your help but this is not what i was looking for. However, it will be a nice addition if the echo shows as well how many times the most used numbers was counted. Ex. 36(found 50 times) 24(found 47 times) 13(found 45 times) 22(found 42 times) 12(found 50 times) 10(found 40 times)
|
trillianjedi

msg:4116346 | 2:49 pm on Apr 15, 2010 (gmt 0) |
It would be a lot easier to help I think if you can post the code that you have so far, and let us know where you got stuck ?
|
pmells

msg:4116361 | 3:26 pm on Apr 15, 2010 (gmt 0) |
Try this:
<?php
$lines = file('test.txt'); $numsArr = array(); $lastArr = array(); foreach($lines as $line) { $line = trim($line); $nums = explode(" ", $line); $t = count($nums); $n = 0; while($n < ($t-1)) { $num = $nums[$n]; if(!is_array($numsArr[$num])) { $numsArr[$num] = array(); } $numsArr[$num][] = $num; $n++; } $last = $nums[$t-1]; if(!is_array($lastArr[$last])) { $lastArr[$last] = array(); } $lastArr[$last][] = $last; } $limit = 5; $totalNums = array(); foreach($numsArr as $key => $value) { $c = count($value); if(!is_array($totalNums[$c])) { $totalNums[$c] = array(); } $totalNums[$c][] = $key; } $amounts = array(); foreach($totalNums as $key => $value) { $amounts[] = $key; } $most = max($amounts); $topNums = array(); $lefttofind = $limit - count($totalNums[$most]); $n = 0; while($lefttofind > 0 or $n < $limit) { if($lefttofind <= 0) { $topNums[$most][$n] = $totalNums[$most][$n]; $n++; } else { $dif = $limit - $lefttofind; while($n < $dif) { $topNums[$most][$n] = $totalNums[$most][$n]; $n++; } unset($totalNums[$most]); $amounts = array(); foreach($totalNums as $key => $value) { $amounts[] = $key; } $most = max($amounts); $lefttofind = $lefttofind - count($totalNums[$most]); } } $totalLast = array(); foreach($lastArr as $key => $value) { $c = count($value); if(!is_array($totalLast[$c])) { $totalLast[$c] = array(); } $totalLast[$c][] = $key; } $amounts = array(); foreach($totalLast as $key => $value) { $amounts[] = $key; } $most = max($amounts); $topLast = "The folling last number appeared " . $most ." times: " . $totalLast[$most][0]; foreach($topNums as $key => $value) { echo "The following number(s) appeared " . $key . " times: "; foreach($value as $num) { echo $num . " "; } echo "<br />\n"; } echo "<hr />"; echo $topLast; ?>
|
pmells

msg:4116362 | 3:27 pm on Apr 15, 2010 (gmt 0) |
p.s. Yes it's a little messy, I don't, right now, have time to clean it up or comment...
|
nimonogi

msg:4116363 | 3:28 pm on Apr 15, 2010 (gmt 0) |
@trillianjedi The reason i didn't post the code it's because i've made it all wrong and complicated. I don't want to continue fixing things on that code. I prefer to see a different approach by a professional.
|
nimonogi

msg:4116368 | 3:32 pm on Apr 15, 2010 (gmt 0) |
pmells thank you very much for your effort. Here is the output i'm getting: The following number(s) appeared 167 times: 37 The following number(s) appeared 158 times: The following number(s) appeared 157 times: The following number(s) appeared 155 times: The following number(s) appeared 154 times: ---------------------------------------------- The folling last number appeared 78 times: 6
|
pmells

msg:4116371 | 3:36 pm on Apr 15, 2010 (gmt 0) |
Have you got "random" white space in your file? I have tested it with the sample data you gave at the top and it worked fine. Can you send me a wider set of sample data? I'll sticky you my email.
|
Readie

msg:4116372 | 3:39 pm on Apr 15, 2010 (gmt 0) |
<?php
$stuff = file('list_of_numbers.txt'); foreach($stuff as $stu) { $seps = explode(" ", $stu); $count = count($seps); $coun = ($count - 1); for($i = 0; $i < $coun; $i++) { $ident = 'a' . $seps[$i]; if(isset($check_first[$ident])) { $check_first[$ident] += 1; } else { $check_first[$ident] = 1; } } $ident = 'a' . rtrim($seps[$coun]); if(isset($check_last[$ident])) { $check_last[$ident] += 1; } else { $check_last[$ident] = 1; } } arsort($check_first); arsort($check_last); $output = '<p><b>Most common first numbers:</b><br />'; reset($check_first); for($i = 0; $i < 5; $i++) { $output .= ltrim(key($check_first), "a"); if($i < 4) { $output .= ' '; } next($check_first); } reset($check_last); $the_key = ltrim(key($check_last), "a"); $output .= '</p><p><b>Most common last number:</b><br />' . $the_key; echo $output;
?> |
| Try that.
|
nimonogi

msg:4116375 | 3:42 pm on Apr 15, 2010 (gmt 0) |
@pmells You can download it at <snip> [edited by: dreamcatcher at 6:06 pm (utc) on Apr 15, 2010] [edit reason] No download urls please. See TOS. [/edit]
|
nimonogi

msg:4116380 | 3:49 pm on Apr 15, 2010 (gmt 0) |
Readie thanks a lot! Works exactly as i wanted. Is it possible to count how many times each of these numbers had been used as well?
|
Readie

msg:4116386 | 3:50 pm on Apr 15, 2010 (gmt 0) |
Just read how you want the count to appear. Modified my code and tested. Works fine: <?php
$stuff = file('list_of_numbers.txt'); foreach($stuff as $stu) { $seps = explode(" ", $stu); $count = count($seps); $coun = ($count - 1); for($i = 0; $i < $coun; $i++) { $ident = 'a' . $seps[$i]; if(isset($check_first[$ident])) { $check_first[$ident] += 1; } else { $check_first[$ident] = 1; } } $ident = 'a' . rtrim($seps[$coun]); if(isset($check_last[$ident])) { $check_last[$ident] += 1; } else { $check_last[$ident] = 1; } } arsort($check_first); arsort($check_last); $output = '<p><b>Most common first numbers:</b><br />'; reset($check_first); for($i = 0; $i < 5; $i++) { $output .= ltrim(key($check_first), "a") . ' (' . current($check_first) . ')'; if($i < 4) { $output .= '<br />'; } next($check_first); } reset($check_last); $the_key = ltrim(key($check_last), "a"); $output .= '</p><p><b>Most common last number:</b><br />' . $the_key . ' (' . current($check_last) . ')'; echo $output;
?> |
|
|
nimonogi

msg:4116490 | 6:28 pm on Apr 15, 2010 (gmt 0) |
Thank you. Problem solved :)
|
|