开发者

parsing a text file in javascript? Or something better?

I am wondering how I might parse a text file in Javascript and create counts for each item in the text file. Example, a text file that contains:

apple

orange

banana

apple

apple

peach

and the output I want to collect is something like:

apple 3

orange 1

banana 1

peach 1

Javascrip开发者_如何学编程t might not be the best since this is literally millions of rows like this. I could do it in something else like PHP and store the results in MySQL too. I want to run this routine as items are added to the file and snapshot the results after each run for historical comparison.

What is the best way to accomplish this?


It sounds like work for a database. You could count the items in a database table in milliseconds, while parsing a text file using compiled code would take seconds, and using client side Javascript would take several minutes.

A database query to group and count the items is quite simple:

select Fruit, count(*)
from FruitTable
group by Fruit

Edit:

I created a test table in SQL Server with 8 million records, and the above query runs in about three seconds.


This is with PHP

$string = file_get_contents ("textfile.txt");

$string_array = explode("\n",$string); foreach($string_array as $value) {
        if(isset($str_count[$value]))
                $str_count[$value]++;
        else
                $str_count[$value]=1; } foreach($str_count as $key => $value)
        echo $key.": ".$value."<br>";

textfile.txt:

apple
orange
banana
apple
apple
peach

Output:

apple: 3
orange: 1
banana: 1
peach: 1


use perl, an example:

#!/usr/bin/perl -w
use strict;

my $file   = shift;
my %fruits = ();

open(my $handle, "<", $file);

while(defined(my $fruit = <$handle>)) {
        chomp $fruit;
        $fruits{$fruit}++;
}

foreach my $key (sort keys %fruits) {
        print "$key: ", $fruits{$key}, "\n";
}

then run it with perl fruits.pl FILENAME_TO_FRUITS_FILE

ultimately, the above is the easiest way to do it, but you really should move it to a database if you do have millions of entries. any simple PHP/MySQL tutorial will be helpful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜