Script to add numbers and display the total

Script to add numbers and display the total

14
Basic UserBasic User
14

    May 08, 2012#1

    I'd like to add numbers that always appear after the same set of characters in a string and display the total.

    Code: Select all

    xyz.htm' target='sub_results'>27 time(s)
    abcedgy.htm' target='sub_results'>3 time(s)
    123.htm' target='sub_results'>2 time(s)
    668bp6.htm' target='sub_results'>2 time(s)
    a.htm' target='sub_results'>1 time(s)
    In this case it would be 27+3+2+2+1=35 which is pretty simple to add up. But I have various files with from 250 to 3700 lines and would like to be able to get totals from them.

    The common thing is target='sub_results'>number from 1 to 5000 time and there is not more than one occurrence of the string per line.

    Is this a feasible idea? Thanks.

    6,675585
    Grand MasterGrand Master
    6,675585

      May 09, 2012#2

      The following script should work. I tested it on your example with UE v18.00.0.1034. It is not clear for me if there is one file containing all the results or if you want to get the results from a set of files. The script expects that the lines with the results are in active file on script execution. You may need first to run a Find in Files with Results to Edit Window if you first need to get all lines with results from a set of files.

      Code: Select all

      if (UltraEdit.document.length > 0) {  // Is at least 1 file opened?
         // Define environment for the script.
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
         // Move caret to top of active file.
         UltraEdit.activeDocument.top();
         // Define the parameters for the case-sensitive Perl regular
         // expression Find command used to find the numbers to sum.
         UltraEdit.perlReOn();
         UltraEdit.activeDocument.findReplace.mode=0;
         UltraEdit.activeDocument.findReplace.matchCase=true;
         UltraEdit.activeDocument.findReplace.matchWord=false;
         UltraEdit.activeDocument.findReplace.regExp=true;
         UltraEdit.activeDocument.findReplace.searchDown=true;
         if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean")
            UltraEdit.activeDocument.findReplace.searchInColumn=false;
         // Initialize the 2 number variables for the sum and the counter for found results.
         var nSum = 0;
         var nCount = 0;
         // Run from top of file to bottom a case-sensitive Perl regular expression
         // Find searching for numbers after fixed string "target='sub_results'>"
         // using a lookbehind to select just the found number by the Find command.
         while (UltraEdit.activeDocument.findReplace.find("(?<=target='sub_results'>)\\d+")) {
            // Convert the found number string to an integer number and add it to existing sum.
            nSum += parseInt(UltraEdit.activeDocument.selection,10);
            nCount++;  // Increase the counter by 1 on every found results number.
         }
         // Display the user of the script the sum with a message box.
         UltraEdit.messageBox("Sum of " + nCount + " results is " + nSum);
      }

      14
      Basic UserBasic User
      14

        May 09, 2012#3

        Thanks once again Mofi. This accomplishes exactly what I wanted. Works great!

        Cheers from Ottawa! :D