How to reverse a list of numbers within a line and on multiple lines?

How to reverse a list of numbers within a line and on multiple lines?

3

    Jan 07, 2021#1

    Hello, I wasn't sure where to put this but I was wondering if someone could help me with how to reverse a list of numbers horizontally and vertically.

    For example, I have this list: 4000, 6875, 6625, 2305
    But I want the numbers listed as: 23056625, 6875, 4000

    Or I have this list of numbers:

    4000,
    6875,
    6625,
    2305

    But I want the number listed as:

    2305,
    6625,
    6875,
    4000

    How could this be done with an UltraEdit script?

    6,676579
    Grand MasterGrand Master
    6,676579

      Jan 08, 2021#2

      Here is a commented UltraEdit script for both tasks as it reverses the order of the numbers within one or more selected lines and also the order of all selected lines. Everything in active file is selected if there is no selection on starting the script.

      Code: Select all

      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
         // Define environment for this script.
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
      
         // Select everything in active file if there is nothing selected on script start.
         if (!UltraEdit.activeDocument.isSel()) UltraEdit.activeDocument.selectAll();
      
         // Is the active file not an empty file?
         if (UltraEdit.activeDocument.isSel())
         {
            // Get type of line termination of active file as string.
            var sLineTerm;
            if (UltraEdit.activeDocument.lineTerminator < 1) sLineTerm = "\r\n";
            else if (UltraEdit.activeDocument.lineTerminator == 1) sLineTerm = "\n";
            else sLineTerm = "\r";
      
            // Get the selected block as an array of strings
            // with each string being a line of the selected block.
            var asInputLines = UltraEdit.activeDocument.selection.split(sLineTerm);
      
            // Remove the last string from the array if it is an empty string
            // because of selected block ends with a line termination and in this
            // case remember that the last selected line has also a line termination
            // as needed finally on writing back the output lines to the active file.
            var sLastLineTerm = "";
            if (!asInputLines[asInputLines.length-1].length)
            {
               sLastLineTerm = sLineTerm;
               asInputLines.pop();
            }
      
            // Create an array of strings each consisting of the non-digit
            // characters at end of each line. If all lines with all trailing
            // horizontal tabs and normal spaces removed end with a number, a
            // single comma or a single semicolon, the line ends of the input
            // lines are appended later to the reverse ordered lines.
            // Otherwise the line end of each line is kept as is.
            var asLineEnds = [];
            var nCharCode;
            var nCharPos;
            var nLineNumber;
            var nNextLineEnd = 1;
      
            for (nLineNumber = 0; nLineNumber < asInputLines.length; nLineNumber++)
            {
               nCharPos = asInputLines[nLineNumber].length;
               // Find the last digit character in the current line.
               while ((--nCharPos) >= 0)
               {
                  nCharCode = asInputLines[nLineNumber].charCodeAt(nCharPos);
                  if ((nCharCode >= 0x30) && (nCharCode <= 0x39)) break;
               }
               nCharPos++;
               // Does the line end with a number?
               if (nCharPos >= asInputLines[nLineNumber].length)
               {
                  asLineEnds.push("");    // Append an empty string to the array.
               }
               else
               {
                  // Get the string at end of the line after last digit.
                  var sLineEnd = asInputLines[nLineNumber].substr(nCharPos);
                  // Append this non-empty string to the array of line ends.
                  asLineEnds.push(sLineEnd);
                  // Remove all trailing horizontal tabs and normal spaces.
                  sLineEnd = sLineEnd.replace(/[\t ]+$/,"");
                  // Is the line end now not an empty string?
                  if (sLineEnd.length)
                  {
                     // Is the line end not a single comma or semicolon?
                     if ((sLineEnd != ",") && (sLineEnd != ";")) nNextLineEnd = -1;
                  }
               }
            }
      
            var asOutputLines = [];
            var nLineEndNumber = (nNextLineEnd > 0) ? 0 : asInputLines.length - 1;
            var nLineLength;
            var nStartPos;
      
            // Reverse now the non-negative integer numbers within a line and
            // reverse at the same time also the lines itself. A hyphen-minus
            // character left to a number is not interpreted as minus sign of
            // an integer number. So this script can be used only for positive
            // integer numbers. The non-digit characters at beginning of each
            // line are kept as also the non-digit characters between the numbers.
            // The non-digit characters at end of each line are taken from either
            // the current line or the original line in the selected block according
            // to line number of the line in the array of the output lines.
            for (nLineNumber = asInputLines.length - 1; nLineNumber >= 0; nLineNumber--)
            {
               nLineLength = asInputLines[nLineNumber].length;
               // There is nothing to really do for an empty line.
               if (!nLineLength)
               {
                  asOutputLines.push(asLineEnds[nLineEndNumber]);
                  nLineEndNumber += nNextLineEnd;
                  continue;
               }
      
               // Get the numbers within the line and the other
               // strings separated into two arrays of strings.
               var asNumbers = [];
               var asOthers = [];
               var bNumber = false;
      
               // It must be first determined if the current line starts with
               // a number or has other characters at beginning of the line.
               nStartPos = 0;
               nCharCode = asInputLines[nLineNumber].charCodeAt(nStartPos);
               if ((nCharCode >= 0x30) && (nCharCode <= 0x39))
               {
                  asOthers.push("");
                  bNumber = true;
               }
      
               // Process the line character by character.
               for (nCharPos = 1; nCharPos < nLineLength; nCharPos++)
               {
                  nCharCode = asInputLines[nLineNumber].charCodeAt(nCharPos);
                  if ((nCharCode >= 0x30) && (nCharCode <= 0x39))
                  {
                     if (bNumber) continue;
                     bNumber = true;
                     // Append the current other string to array as last element.
                     asOthers.push(asInputLines[nLineNumber].substring(nStartPos,nCharPos));
                     nStartPos = nCharPos;
                  }
                  else
                  {
                     if (!bNumber) continue;
                     bNumber = false;
                     // Insert the current number in array as first element.
                     asNumbers.unshift(asInputLines[nLineNumber].substring(nStartPos,nCharPos));
                     nStartPos = nCharPos;
                  }
               }
               if (bNumber)   // Does the line end with a number?
               {
                  asNumbers.unshift(asInputLines[nLineNumber].substring(nStartPos,nLineLength));
               }
      
               // Rebuild the line with the numbers in reverse order.
               var sLine = "";
               for (var nIndex = 0; nIndex < asNumbers.length; nIndex++)
               {
                  sLine += asOthers[nIndex] + asNumbers[nIndex];
               }
      
               // Append the line ending of the appropriate line which is either
               // the line ending of the current line or the line ending of the
               // input line according to line number of current line in the
               // array of output lines.
               sLine += asLineEnds[nLineEndNumber];
               nLineEndNumber += nNextLineEnd;
      
               // Append the rebuild line to the array of the output lines.
               asOutputLines.push(sLine);
            }
      
            // Join the output lines together to one string with line termination
            // inserted between and append also a line termination if the current
            // selection in active file ends with a line termination and overwrite
            // the existing selection.
            UltraEdit.activeDocument.write(asOutputLines.join(sLineTerm) + sLastLineTerm);
         }
      }
      
      Best regards from an UC/UE/UES for Windows user from Austria