循环文件并计算所选实例

I have this file and i want to count the number of instances for each "ROBxx". The file can contain thousands of lines. What is the easiest way to loop trough and count?

2012.05.08 11:15:49 ROB52 4mm

2012.05.08 11:15:56 ROB42 5mm

2012.05.08 11:15:59 ROB30 4mm

2012.05.08 11:16:01 ROB52 4mm

2012.05.08 11:16:04 ROB42 2mm

2012.05.08 11:16:05 ROB06 4mm

2012.05.08 11:16:06 ROB52 4mm

2012.05.08 11:16:10 ROB52 4mm

2012.05.08 11:16:11 ROB30 3mm

The result of this file after count should be:

ROB52: 4

ROB42: 2

ROB30: 2

ROB06: 1

Thanks!

First, create a 2-Dimensional Array (or list or whatever) with a String and a integer. Now you can loop through each line and do following:

Cut out the "ROBxx" String and save it as String.

Check the 2-Dimensional Array for the String. if you found it, increase the count. If not already in the Array, add it and set the count to 1.

Hope this helps.

$string = file_get_contents(filename)
$count = preg_match_all('/ROB[0-9]{2}/', $string, $aMatches);
$handle = fopen("input.txt", "r");
$hash = array();
if ($handle) {
        while (($buffer = fgets($handle)) !== false) {
                if(preg_match('/\b(ROB\d+)\b/',$buffer,$m)) {
                        $hash[$m[1]] = (isset($hash[$m[1]])?($hash[$m[1]]+1):1);
                }
        }
        foreach($hash as $k=>$v) {
                print "$k : $v
";
        }
} else {
        // error openeing file.
}