本帖最后由 思想之翼 于 2023-6-30 15:43 编辑
下述代码将文本记录数值,按出现次数排序:
331 445 550 567
008 023 327 550 689
142 350 331 449 550 653
结果为:
550 (出现3次)
331 (出现2次)
445 550 567 008 023 327 689 142 350 449 653 (出现1次)- @set @n=0;/* & echo off
- set "x=1000"
- md "文件夹2" 2>nul
- pushd "文件夹1\"
- dir /b *.txt|cscript -nologo -e:jscript "%~0" "%x%"
- pause & exit/b & rem */
-
- fso = new ActiveXObject("Scripting.FileSystemObject");
- while (!WScript.StdIn.AtEndOfStream) {
- f = WScript.StdIn.ReadLine();
- try {
- txt = fso.OpenTextFile(f, 1).ReadAll();
- txt = getMaxNum(txt, WScript.Arguments(0));
- } catch(e) { txt = "" }
- fso.CreateTextFile("..\\文件夹2\\"+f, true).Write(txt);
- }
-
- function getMaxNum(str, arg) {
- dic = new ActiveXObject("Scripting.Dictionary");
- for (i=1000; i<=1999; i++) {
- num = (i + "").substr(1);
- k = str.split(num).length;
- if (k > 1) {
- if (dic.Exists(k)) {
- dic.Item(k) += " " + num;
- } else dic.Add(k, num)
- }
- }
-
- ar = (new VBArray(dic.Keys())).toArray();
- s = "";
-
- ar.sort(function(x,y){return y-x});
- if (arg > ar.length) arg = ar.length;
- for (i=0; i<arg; i++) {
- s += dic(ar[i]) + "\r\n";
- }
- return s
- }
复制代码 问题:若文本记录数值达3GB,该代码出错。
请教:有无其他方法,实现按出现次数排序? |