summation of numbers at each row ?

summation of numbers at each row ?

5
NewbieNewbie
5

    Feb 22, 2013#1

    There are thousands of rows of English characters followed by two or more sets of numbers separated with “:”(see below). I’m wondering, is it possible to get the sum of the sets of numbers. Take Row One (abc 111:222:333) for example, can Ultraedit change Row One into “abc 666”. Thanks a million.


    ......
    abc 111:222:333
    aa 22875:185
    aaaaa 1935:740:4435
    a 783:494:480:121
    aaaaaaa 277:25:238
    bb 254:580:121
    bbbbb 243:23:211
    b 480:125:185
    bbbbbbb 131:1027:740:121
    cc 3598:264
    ccccc 203:55
    c 71:217
    ccccccc 2054:2308
    ccc 274:1776::121
    aaa 868:1261:185
    bbb 4435:50:00
    .....

    6,606547
    Grand MasterGrand Master
    6,606547

      Feb 24, 2013#2

      Here is a script to convert your example to:

      abc 666
      aa 23060
      aaaaa 7110
      a 1878
      aaaaaaa 540
      bb 955
      bbbbb 477
      b 790
      bbbbbbb 2019
      cc 3862
      ccccc 258
      c 288
      ccccccc 4362
      ccc 2171
      aaa 2314
      bbb 4485


      The script code:

      Code: Select all

      if (UltraEdit.document.length > 0) { // Is any file opened?
      
         // Define environment for this script.
         UltraEdit.insertMode();
         UltraEdit.columnModeOff();
         UltraEdit.activeDocument.hexOff();
         UltraEdit.ueReOn();
      
         /* Verify the line termination of the last line of the file
            and insert one if the last line has no line termination. */
         UltraEdit.activeDocument.bottom();
         if (UltraEdit.activeDocument.isColNumGt(1)) {
            UltraEdit.activeDocument.insertLine();
            if (UltraEdit.activeDocument.isColNumGt(1)) {
               UltraEdit.activeDocument.deleteToStartOfLine();
            }
         }
         UltraEdit.activeDocument.top();
         UltraEdit.activeDocument.trimTrailingSpaces();
      
         // Define line terminator type by default for DOS. If the version of
         // UE/UES has support for the line terminator property, use it to set
         // the line terminator string according to the value of this property.
         var sLineTerm = "\r\n";
         if (typeof(UltraEdit.activeDocument.lineTerminator) == "number") {
            if (UltraEdit.activeDocument.lineTerminator == 1) {
               sLineTerm = "\n";  // UNIX file not converted to DOS.
            }  // Ignore older versions of UltraEdit and MAC files for this
         }     // script and use in all other cases also DOS line terminators.
      
         UltraEdit.selectClipboard(9);
         // Process entire file in blocks of 10000 lines to end of file.
         var nLineNum = 1;
         do {
            nLineNum += 10000;
            UltraEdit.activeDocument.gotoLineSelect(nLineNum,1);
            if (!UltraEdit.activeDocument.isSel()) break;
            var asLines = UltraEdit.activeDocument.selection.split(sLineTerm);
      
            // Process each line of the selected block.
            for (var nIndex = 0; nIndex < asLines.length; nIndex++ ) {
      
               // Find first character of a sequence of colon delimited numbers at end of the line.
               var nNumberPos = asLines[nIndex].search(/(?:\d+:*)+$/);
               // If no number found, for example a blank line, skip the line.
               if (nNumberPos < 0) continue;
               // Split the line into fixed text and the numbers as string.
               var sText = asLines[nIndex].substr(0,nNumberPos);
               var sNumbers = asLines[nIndex].substr(nNumberPos);
               // Split the numbers string up into an array of strings each with a number.
               var asNumbers = sNumbers.split(":");
               // If the line contains only 1 number, nothing to do for this line.
               if (asNumbers == null) continue;
      
               // Convert the number strings into integers and sum them.
               var nSum = 0;
               for (var nNumber = 0; nNumber < asNumbers.length; nNumber++ ) {
                  // Skip empty strings caused by :: between the numbers.
                  if (asNumbers[nNumber].length == 0) continue;
                  nSum += parseInt(asNumbers[nNumber],10);
               }
               // Build new line with fixed text and the sum converted to a decimal number string.
               asLines[nIndex] = sText + nSum.toString(10);
            }
            // Rebuild the block with the modified lines in user clipboard 9.
            UltraEdit.clipboardContent = asLines.join(sLineTerm);
            // Paste the block over the still selected lines.
            UltraEdit.activeDocument.paste();
         } while (UltraEdit.activeDocument.isEof() == false);
      
         // Clear user clipboard 9 and select Windows clipboard.
         UltraEdit.clearClipboard();
         UltraEdit.selectClipboard(0);
      }

      5
      NewbieNewbie
      5

        Feb 27, 2013#3

        It works brilliantly and fast.

        Thank you!