有一个日志文件格式是这样的4129,23|4074,25|4011,16|……每组以|分隔,前者是更新的模板ID,后者是该模板被关联的频道,需要统计模板的关联频道数并排序。

输入: 4129,23|4074,25|4011,16|…… 输出: 模板ID 关联频道数 3261 871 523 824 2039 811 ……

html代码

<form>
<input type="file" size="30" id="filePath" />
<input type="button" value="分析" id="analyze" />
</form>

javascript处理代码

function $(el){ return document.getElementById(el);}
var analyze = function(){
    this.analyze = 'analyze';
    this.filePath = 'filePath';
    this.readFlag = 1;
    this.writeFlag = 2;
    //匹配数据组的正则表达式
    this.regPattern = /^(d+),(d+)$/;
    this.cache = {result:{},array:[]};
    this.fso = new ActiveXObject("Scripting.FileSystemObject");
};
analyze.prototype = {
    start:function(){
        var _filePath = $(this.filePath).value;
        var pairArray = this.read(_filePath).split('|'),pair = null;
        for(var i = 0 ; i < pairArray.length ; i++){
            //如果正则表达式匹配成功
            //则pair[0]为全匹配,pair[1]为第一个子匹配,pair[2]为第二个子匹配
            pair =  pairArray[i].match(this.regPattern);
            if(pair){
                if(this.cache.result[pair[1]] != undefined && this.cache.result[pair[1]] != null)
                    this.cache.result[pair[1]]++;
                else
                    this.cache.result[pair[1]] = 1;
            }
        }
        //从result拷贝到array,方便快速排序
        for(var i in this.cache.result){
            this.cache.array.push({templateId:i,channelCount:this.cache.result[i]});
        }    
        this.cache.array = this.quickSort(this.cache.array);
        this.write(this.quickSort(this.cache.array),_filePath.replace('.txt','')+'_statistics.txt');
        alert('done!');
    },
    read:function(filePath){
        var _file = this.fso.OpenTextFile(filePath, this.readFlag);
        var _string = _file.ReadAll();
        _file.close();
        return _string;
    },
    write:function(array,filePath){
        var _file = this.fso.OpenTextFile(filePath, this.writeFlag,true);
        _file.WriteLine('模板ID    关联频道数');
        for(var i in array){
            _file.WriteLine(this.digitFix(array[i]['templateId'],4)
                +'     '+ this.digitFix(array[i]['channelCount'],4));
        }
        _file.close();
    },
    quickSort:function(array,left,right){
        if(left == null && right == null){ left = 0; right = array.length-1;}
        var swap = function(i,j){
            if(i==j) return;
            var temp = _array[i];
            _array[i] = _array[j];
            _array[j] = temp;
        }
        var i, last,_array = array;
        if (left >= right) return _array;
        last = left;
        for (i = left + 1; i <= right;i++ ){
            if(this.compare(_array[i], _array[left]))
                swap(++last,i);
        }
        swap(left,last);
        this.quickSort(_array,left,last-1);
        this.quickSort(_array,last+1,right);
        return _array;
    },
    compare:function(x,y){
        return x['channelCount'] > y['channelCount'];
    },
    digitFix:function(number,count){
        var _string = number + '';
        var _count = count-_string.length;
        for(var i = 0; i < _count; i++)
            _string = ' ' + _string;
        return _string;
    },
    bind:function(el,type,fn,range,params){
        var _params = params == null || params.constructor != Array  ? [params] : params;
        if (el.addEventListener)
            el.addEventListener(type,function(){fn.apply(range,_params)}, false);
        else
            el.attachEvent("on"+type, function(){fn.apply(range,_params)});
    },
    init:function(){
        this.bind($(this.analyze),'click',this.start,this);
    }
};
var aa = new analyze();
aa.init();