Tapatalk

Counting how often each string in a list of strings exists in a file

Counting how often each string in a list of strings exists in a file

24
Basic UserBasic User
24

    May 13, 2013#1

    This script isn't working 100%. It should count numbers (01 to 00, as 00 is like 100, or 01 to 99, then 00) and should display the total of each number.

    For example:
    01 = 3000
    02 = 2994
    ...
    00 = 2999

    Code: Select all

    if (UltraEdit.document.length > 0)
    {
       UltraEdit.insertMode();
       if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
       else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
       UltraEdit.activeDocument.hexOff();
       // Get current caret position in active file.
       var nActLineNum = UltraEdit.activeDocument.currentLineNum;
       var nActColNum = UltraEdit.activeDocument.currentColumnNum;
       if (typeof(UltraEdit.activeDocumentIdx) == "undefined") nActColNum++;
       // Select the entire file and load contents into a string variable.
       UltraEdit.activeDocument.selectAll();
       if (UltraEdit.activeDocument.isSel()) {   // Is the file not empty?
          var sAllLines = UltraEdit.activeDocument.selection;
          // Restore initial caret position.
          UltraEdit.activeDocument.gotoLine(nActLineNum,nActColNum);
          // Get first line from string variable.
          var nFirstLineTerm = sAllLines.indexOf("\r\n");
          if (nFirstLineTerm > 0)   // Has the file at least 1 line termination
          {                         // and the first line is not empty line?
             var sFirstLine = sAllLines.substring(0,nFirstLineTerm);
             // Replace all occurrences of 2 or more spaces by a single space.
             sFirstLine = sFirstLine.replace(/  +/g," ");
             // Split the first line up into an array of strings at every space.
             var asNumbers = sFirstLine.split(' ');
             var sResult = "";
             for (var nNumber = 0; nNumber < asNumbers.length; nNumber++)
             {
                // Ignore empty strings and strings not starting with a digit.
                if (!asNumbers[nNumber].length) continue;
                if ((asNumbers[nNumber][0] < '0') || (asNumbers[nNumber][0] > '9')) continue;
                // Use match function of String object to find out how often
                // each string in first line exists in total on all lines.
                var rSearch = new RegExp(asNumbers[nNumber], 'g');
                var asFound = sAllLines.match(rSearch);
                // Build the output string.
                if (nNumber > 0) sResult += "\n";
                sResult += asNumbers[nNumber] + " = " + asFound.length;
             }
             // Print the results into the output window.
             UltraEdit.outputWindow.clear();
             UltraEdit.outputWindow.showStatus=false;
             if (UltraEdit.outputWindow.visible == false)
             {
                UltraEdit.outputWindow.showWindow(true);
             }
             UltraEdit.outputWindow.write(sResult);
          }
       }
    }
    But 50% of numbers are being skipped. It doesn't count them all.

    Numbers are like 01 03 05 07 08 09 10 15 28 34 38 42 47 51 ... 99 00 (each line ends with an enter).

    Real result (limited to 50 numbers, and should be 100):

    Code: Select all

    01 = 3003
    02 = 3003
    03 = 3003
    04 = 3003
    05 = 3003
    06 = 3003
    11 = 5598
    12 = 4402
    15 = 4987
    16 = 4576
    17 = 5662
    19 = 3806
    24 = 3898
    26 = 5160
    27 = 6330
    28 = 4484
    29 = 2854
    31 = 3531
    37 = 5314
    38 = 6012
    39 = 5307
    41 = 5670
    43 = 5656
    47 = 5600
    48 = 5625
    49 = 6350
    50 = 4866
    51 = 5530
    54 = 4899
    55 = 4756
    58 = 3969
    61 = 4675
    62 = 4987
    63 = 4592
    64 = 5335
    67 = 2694
    68 = 3884
    73 = 5324
    74 = 4290
    76 = 5555
    77 = 2540
    78 = 4474
    85 = 5488
    86 = 5007
    87 = 5409
    89 = 2872
    90 = 2584
    92 = 6203
    97 = 6538
    98 = 5266

    6,685587
    Grand MasterGrand Master
    6,685587

      May 14, 2013#2

      Well, the script was written to count how often each number in the first line of the file exists in total in entire file.

      You should not just use the scripts I wrote for you, but also try to understand them and learn the JavaScript syntax so that you can modify the scripts for other purposes by yourself.

      The script below counts how often each often the 100 number strings possible in your files exist in total in active file.

      Code: Select all

      if (UltraEdit.document.length > 0)
      {
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
         UltraEdit.activeDocument.hexOff();
         // Get current caret position in active file.
         var nActLineNum = UltraEdit.activeDocument.currentLineNum;
         var nActColNum = UltraEdit.activeDocument.currentColumnNum;
         if (typeof(UltraEdit.activeDocumentIdx) == "undefined") nActColNum++;
         // Select the entire file and load contents into a string variable.
         UltraEdit.activeDocument.selectAll();
         if (UltraEdit.activeDocument.isSel()) {   // Is the file not empty?
            var sAllLines = UltraEdit.activeDocument.selection;
            // Restore initial caret position.
            UltraEdit.activeDocument.gotoLine(nActLineNum,nActColNum);
            // Create an array with 100 number strings "01","02", ..., "99","00".
            var nNumber = 1;
            var asNumbers = new Array(101);
            do   // The first 9 number strings with a leading zero.
            {
               asNumbers[nNumber] = "0" + nNumber.toString(10);
            }
            while(++nNumber < 10); 
            do   // The number strings for "10" to "99" can be created simply.
            {
               asNumbers[nNumber] = nNumber.toString(10);
            }
            while(++nNumber < 100); 
            asNumbers[100] = "00";   // Number string "00" for 100.
            // Count how often each of the 100 numbers exist in entire file.
            var sResult = "";
            for (nNumber = 1; nNumber <= 100; nNumber++)
            {
               var rSearch = new RegExp(asNumbers[nNumber], 'g');
               var asFound = sAllLines.match(rSearch);
               // Build the results string written to the output window.
               if (nNumber > 1) sResult += "\n";
               if (asFound == null) sResult += asNumbers[nNumber] + " = 0";
               else sResult += asNumbers[nNumber] + " = " + asFound.length.toString(10);
            }
            // Print the results into the output window.
            UltraEdit.outputWindow.clear();
            UltraEdit.outputWindow.showStatus=false;
            if (UltraEdit.outputWindow.visible == false)
            {
               UltraEdit.outputWindow.showWindow(true);
            }
            UltraEdit.outputWindow.write(sResult);
         }
      }

      24
      Basic UserBasic User
      24

        May 14, 2013#3

        Thanks a lot Mofi !