**文書の過去の版を表示しています。**
indexof is not a good way
var fs = require("fs"); var data = fs.readFileSync('/System/Volumes/Data/home/2544842260/Public/100again.txt'); var arr = data.toString().split("\n"); //arr.pop(); arr.sort(); var tmp = arr; var wordcounter = []; var wordlist=[] var savelist=[] //words counting var wordcountertmp=[] var wordlisttmp=[] for (var s = 0; s < tmp.length; s ++ ){ posiOf1stSemicolon=tmp[s].indexOf(";"); var wordtmp = tmp[s].slice(0,posiOf1stSemicolon) //check if it is a new word. yes - add the new word. no - plus 1 var existchecker = wordlisttmp.indexOf(wordtmp) if(existchecker==-1){ wordlisttmp.push(wordtmp) wordcountertmp.push(1) } else{ var posi=existchecker; wordcountertmp[posi]=wordcountertmp[posi]+1 } //get the most counted word var maxTmp=0 for (i=0;i<wordcountertmp.length;i++){ if (maxTmp<wordcountertmp[i]){ maxTmp=wordcountertmp[i] indexOfMostCountedWordtmp=i } } var mostCountedWordtmp=wordlisttmp[indexOfMostCountedWordtmp] } //save the less counted species, and remove saved/counted item from tmp for(var s = 0; s < tmp.length; s ++){ if (tmp[s].indexOf(mostCountedWordtmp) == -1){ console.log("看这里!") console.log(tmp[s]) savelist.push(tmp[s]) tmp.splice(s,1) } else{ tmp[s]=tmp[s].slice(mostCountedWordtmp.length+1) } } wordcounter.push(wordcountertmp) wordlist.push(wordlisttmp)
so! with associate array
var fs = require("fs"); var data = fs.readFileSync('/System/Volumes/Data/home/2544842260/Public/100again.txt'); var arr = data.toString().split("\n"); arr.sort(); var speciesNumber=arr.length var speciesList={} var processlist=speciesList var savelist=[] var wordList=[] var countList=[] //save the data to array for (var s=0; s<speciesNumber; s++) { var rowKey=s+1 processlist["species"+rowKey]=arr[s].split(";") } //counting words var layer=0 do{ var counttmp=[] var wordListtmp=[] for (var s=0; s<speciesNumber; s++){ var rowKey=s+1 var existchecker="nonexist" for (i=0;i<wordListtmp.length;i++){ console.log("s "+s) console.log("rowkey "+rowKey) console.log("remain "+countNumber) console.log("species "+rowKey) console.log("layer "+layer) if (processlist["species"+rowKey][layer]===wordListtmp[i]){ existchecker="exist" var posilog=i } } if (existchecker=="exist"){ counttmp[posilog]=counttmp[posilog]+1 } else{ wordListtmp.push(processlist["species"+rowKey][layer]) counttmp.push(1) } } //get most counted word var countNumber=0 for (var j=0;j<wordListtmp.length;j++){ if (countNumber<counttmp[j]){ countNumber=counttmp[j] var indexOfCountNumber=j } var mostCountedWord=wordListtmp[indexOfCountNumber] } //keep the most counted species in processlist; //save other to savelist. savelisttmp=[] for (var s=0; s<speciesNumber; s++){ var rowKey=s+1 if (mostCountedWord!==processlist["species"+rowKey][layer]){ savelisttmp.push("species"+rowKey) delete processlist["species"+rowKey] } } if (savelisttmp.length!==0){ savelist.push(savelisttmp) } //save less counted words and numbers to list wordList.push(wordListtmp) countList.push(counttmp) layer=layer+1 speciesNumber=countNumber console.log("processlist length:") console.log(Object.keys(processlist).length) console.log("wordlist:") console.log(wordList) console.log("countlist:") console.log(countList) console.log("times:") console.log(countNumber) console.log("most counted word:") console.log(mostCountedWord) console.log(" ") console.log("savelist:") console.log(savelist) console.log(" ") } while (countNumber>(speciesNumber/8))
but the loop program is not going well
/System/Volumes/Data/home/2544842260/Public/classifyWithAssosiateArray.js:42 wordListtmp.push(processlist["species"+rowKey][layer]) ^ TypeError: Cannot read property '10' of undefined at Object.<anonymous> (/System/Volumes/Data/home/2544842260/Public/classifyWithAssosiateArray.js:42:59) at Module._compile (node:internal/modules/cjs/loader:1108:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10) at Module.load (node:internal/modules/cjs/loader:973:32) at Function.Module._load (node:internal/modules/cjs/loader:813:14) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12) at node:internal/main/run_main_module:17:47 pch12005m:Public 2544842260$