Script for inserting auto-incremented numbers in headers for identifiers starting with same string with zero leading?

Script for inserting auto-incremented numbers in headers for identifiers starting with same string with zero leading?

49
Basic UserBasic User
49

    Feb 14, 2018#1

    Hi,

    I have the favor to ask for an UltraEdit script for inserting auto-incremented numbers in headers for identifiers starting with same string with zero leading?

    The attached file below contains two files with an input and output example.

    How to increment number against ## for same value with zero leading?
    AutoIncrementHeaderIdentifiers.zip (3.55 KiB)   38
    ZIP file with the two example text files for input and expected output.

    6,604547
    Grand MasterGrand Master
    6,604547

      Feb 17, 2018#2

      The simple script without any comments:

      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.top();
      
         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;
         }
      
         var nNumber = 0;
         var sLastId = "";
         var nModCount = 0;
         var nKeptCount = 0;
         
         while (UltraEdit.activeDocument.findReplace.find('(?<=id=")p\\d{2}_s\\d{2}_c\\d{2}_s\\d{2}_\\d{2}_[#\\d]{2}(?=")'))
         {
            var sThisId = UltraEdit.activeDocument.selection.substr(0,19);
            if (sLastId != sThisId)
            {
               nNumber = 0;
               sLastId = sThisId;
            }
            nNumber++;
            var sNumber = (nNumber < 10) ? "0" : "";
            sNumber += nNumber.toString(10);
            var sNewId = sThisId + sNumber;
            if (sNewId != UltraEdit.activeDocument.selection)
            {
               UltraEdit.activeDocument.write(sNewId);
               nModCount++;
            }
            else
            {
               UltraEdit.activeDocument.key("RIGHT ARROW");
               nKeptCount++;
            }
         }
      
         UltraEdit.activeDocument.top();
         var nFoundCount = nModCount + nKeptCount;
         UltraEdit.outputWindow.write("Updated " + nModCount + " and kept " + nKeptCount + " of " +
                                      nFoundCount + " found identifier" + ((nFoundCount!=1) ? "s." : "."));
      }
      
      Best regards from an UC/UE/UES for Windows user from Austria

      49
      Basic UserBasic User
      49

        Feb 18, 2018#3

        Hi Mofi,

        I am grateful to you for this.

        Thanks