1
Name:
Anonymous
2010-05-03 2:24
Trying hard to explode a flat file hit counter.
Here is an example:
home:1|news:33|contact:4
I need to explode first by the | to get the page id and the hits, then I need to explode again by the : to separate them.
I tried using foreach but that was a mess. Any ideas?
4
Name:
Anonymous
2010-05-03 10:48
>>1
explode
Why don't they just call it
split ?
5
Name:
Anonymous
2010-05-03 11:01
>>4
Then they couldn't call the reverse function
implode.
6
Name:
Anonymous
2010-05-03 11:22
>>5
They could have called it
join . But we must forgive them, they came from a language which thought it would be fun to have things like ``
chomp '' and ``slurp mode.''
7
Name:
Anonymous
2010-05-03 15:14
>>6
I don't understand what you're trying to say.
8
Name:
Anonymous
2010-05-03 18:09
>>7
Respectable programmers would never use fun words such as
IsComputerOnFire or
cudder in their works.
Good words to use in a program are
DbReportFactory and
bayesian_average .
11
Name:
Anonymous
2010-05-04 3:31
This is what you want:
"home:1|news:33|contact:4".split("|").map {|x| a,b = x.split(":"); {a => b.to_i} }.inject {|a,b| a.merge! b }
13
Name:
Anonymous
2010-05-04 12:03
<?php
// trollprog.php
function parseHitsFile($filename)
{
$file = file_get_contents($filename);
$pages = explode('|', $file);
$hits = array();
foreach ($pages as $page) {
$tmp = explode(':', $page);
$hits[ $tmp[0] ] = $tmp[1];
}
return $hits;
}
$hitstats = parseHitsFile('hits.txt');
arsort($hitstats);
foreach ($hitstats as $page => $hits) {
echo "$hits — $page";
}
?>