Find and goto longest line

Find and goto longest line

2
NewbieNewbie
2

    Apr 09, 2009#1

    Hi UltraEdit users,

    Sometimes I need a function like goto longest line.
    Actually (in UE 14.20) I try to find them by scrolling down with the page down key :-(
    Can I find them with a regular expression or is there a function built-in?

    Thanks, jrs

    6,687587
    Grand MasterGrand Master
    6,687587

      Apr 14, 2009#2

      There is no built-in function for that task. But following little script works for files not containing tabs or file with horizontal tabs and tab stop value is 1. A tab is one character, but displayed with a variable number of spaces depending on tab stop configuration. But maybe your files do not contain tabs and therefore the script is already enough for you.

      Code: Select all

      // This script sets the cursor to start of the first line in active file
      // with the greatest number of characters. If the file does not contain
      // horizontal tabs, this line is also the first line of the longest lines
      // with the same number of characters or the longest line in file if there
      // is no further line with the same number of characters. The number of
      // characters of longest line is also written into output window.
      
      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
         var nChars = 0;
         var nFound = 0;
         var nMaxFound = 0;
         var nStep  = 1000;
      
         // Define environment for this script.
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
         UltraEdit.activeDocument.hexOff();
         UltraEdit.activeDocument.top();
      
         // Define the parameters for Perl regular expression find.
         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;
         }
      
         while(true)
         {
            nFound = nChars;
            nChars += nStep;
      //      UltraEdit.outputWindow.write("Search for a line with at least " +
      //                                   nChars.toString(10) + " character" +
      //                                   ((nChars != 1) ? "s." : "."));
            if (UltraEdit.activeDocument.findReplace.find("^.{"+nChars+",}"))
            {
               nMaxFound = UltraEdit.activeDocument.selection.length;
      //         UltraEdit.outputWindow.write("Found a line with " + nMaxFound.toString(10) +
      //                                      " character" + ((nMaxFound != 1) ? "s." : "."));
               UltraEdit.activeDocument.gotoLine(0,1);
               nFound = nMaxFound - (nMaxFound % nStep);
            }
            else
            {
               if (nStep == 1) break;
               nStep /= 10;
               nFound = nMaxFound - (nMaxFound % nStep);
            }
            nChars = nFound;
         }
      
         UltraEdit.outputWindow.write("Longest line has " + nFound.toString(10) +
                                      " character" + ((nFound != 1) ? "s." : "."));
      }
      
      Update: The code of the script was updated by me on 2019-30-05 for finding faster the longest line, more precisely the line with greatest number of characters, by reducing the number of finds executed on file.
      Best regards from an UC/UE/UES for Windows user from Austria

      2
      NewbieNewbie
      2

        Apr 15, 2009#3

        Thanks, this script works fine for me.

        Best regards, jrs

        5

          May 29, 2019#4

          I have an XML file, where there should be some lines close to 10.000 letters, and I want to go to the end of the longest line.
          How to?

          Best regards

          Edvard Korsbæk

          6,687587
          Grand MasterGrand Master
          6,687587

            May 30, 2019#5

            Edvard, finding longest line in file cannot be done with a single regular expression find. There is a scripting solution needed for this task. The script in my first post on this topic is one solution.

            I developed today one more solution which uses a different approach by searching first for longest line depending on maximum length entered by the user on script start and reduces the number of characters a line must have at least on no line found with entered maximum line length. This script is also optimized to run as less finds as needed to find in active file the line with greatest number of characters in line.

            Code: Select all

            // This script sets the cursor to start of the first line in active file
            // with the greatest number of characters. If the file does not contain
            // horizontal tabs, this line is also the first line of the longest lines
            // with the same number of characters or the longest line in file if there
            // is no further line with the same number of characters. The number of
            // characters of longest line is also written into output window.
            
            if (UltraEdit.document.length > 0)  // Is any file opened?
            {
               var nMaxLength = 20000;    // This must be an even value greater 0.
            
               // Prompt user for maximum line length and use defined maximun length if
               // user enters 0, 1 or an integer value greater or equal maximun length.
               var nStep = UltraEdit.getValue("Enter maximum line length (default "+
                                              nMaxLength.toString(10) + "):",1);
               if ((nStep < 2) || (nStep >= nMaxLength))
               {
                  nStep = nMaxLength;
               }
               else if (nStep & 1)
               {
                  nStep++;    // Make sure the entered value is even.
               }
            
               var nChars = nStep;
               var nFound = 0;
            
               // Define environment for this script.
               UltraEdit.insertMode();
               if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
               else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
               UltraEdit.activeDocument.hexOff();
               UltraEdit.activeDocument.top();
            
               // Define the parameters for Perl regular expression find.
               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;
               }
            
               do
               {
                  if (nStep & 1) nStep++;
                  nStep /= 2;
            //      UltraEdit.outputWindow.write("Search for a line with at least " +
            //                                   nChars.toString(10) + " character" +
            //                                   ((nChars != 1) ? "s." : "."));
                  if (UltraEdit.activeDocument.findReplace.find("^.{"+nChars+",}$"))
                  {
                     nFound = UltraEdit.activeDocument.selection.length;
            //         UltraEdit.outputWindow.write("Found a line with " + nFound.toString(10) +
            //                                      " character" + ((nFound != 1) ? "s." : "."));
                     UltraEdit.activeDocument.gotoLine(0,1);
                     nChars += nStep;
                     while (nFound >= nChars)
                     {
                        if (nStep & 1) nStep++;
                        nStep /= 2;
                        nChars += nStep;
                     }
                  }
                  else
                  {
                     nChars -= nStep;
                     if (nFound > nChars) break;
                  }
               }
               while (nStep > 1);
            
               UltraEdit.outputWindow.write("Longest line has " + nFound.toString(10) +
                                            " character" + ((nFound != 1) ? "s." : "."));
            }
            
            Best regards from an UC/UE/UES for Windows user from Austria