Arrange numbers and lists the possible cases occurred?

Arrange numbers and lists the possible cases occurred?

12
Basic UserBasic User
12

    Dec 23, 2011#1

    Hi all, I have a data file like this:
    1-Aug
    1 5 7
    2 3

    is there anybody help me write a macro to do this work:
    1. Arrange ascending the numbers:
    Exp:
    1-Aug
    1
    2
    3
    5
    7

    2. Lists the possible cases occurred like this:
    Exp:
    1-Aug
    1 2
    1 3
    1 5
    1 7
    2 3
    2 5
    2 7
    3 5
    3 7
    5 7

    Thanks so much

    6,682583
    Grand MasterGrand Master
    6,682583

      Dec 23, 2011#2

      Your first request can be fulfilled with a macro. The macro property Continue if search string not found must be checked for this macro.

      Code: Select all

      InsertMode
      ColumnModeOff
      HexOff
      UltraEditReOn
      Bottom
      IfColNumGt 1
      InsertLine
      IfColNumGt 1
      DeleteToStartofLine
      EndIf
      EndIf
      Top
      TrimTrailingSpaces
      Find MatchCase RegExp "[ ^t]+"
      Replace All "#"
      Find MatchCase RegExp "%^([0-9]+^)$"
      Replace All "#^1"
      Find MatchCase RegExp "%^([0-9]+#^)"
      Replace All "#^1"
      Find MatchCase RegExp "^([0-9]^)#"
      Replace All "^1^p#"
      Loop 0
      Find MatchCase "#"
      IfNotFound
      ExitLoop
      EndIf
      Key LEFT ARROW
      StartSelect
      Find MatchCase RegExp Select "%[~#^p]"
      IfFound
      Key LEFT ARROW
      EndSelect
      SortAsc Numeric 2 -1 0 0 0 0 0 0
      Find MatchCase RegExp "%[0-9]"
      IfNotFound
      ExitLoop
      EndIf
      Else
      SelectToBottom
      SortAsc Numeric 2 -1 0 0 0 0 0 0
      ExitLoop
      EndIf
      EndLoop
      Top
      Find MatchCase "#"
      Replace All ""
      Here is the macro again with indentations and comments for better understanding how it works. You should save that into a text file with same name as the macro file *.mac containing the compiled macro code, but with file extension uem (or txt).

      Code: Select all

      //  Define the environment for the macro.
          InsertMode
          ColumnModeOff
          HexOff
          UltraEditReOn
      //  Make sure that also last line of file ends with a line termination.
          Bottom
          IfColNumGt 1
              InsertLine
              IfColNumGt 1
                  DeleteToStartofLine
              EndIf
          EndIf
      //  Back to top of the file and delete all trailing spaces.
          Top
          TrimTrailingSpaces
      //  Find strings with 1 or more spaces / tabs and replace
      //  each of them by character #.
          Find MatchCase RegExp "[ ^t]+"
          Replace All "#"
      //  Find lines containing only a number and nothing else
      //  and insert the character # at beginning of such lines.
          Find MatchCase RegExp "%^([0-9]+^)$"
          Replace All "#^1"
      //  Find lines starting with a number followed by character #
      //  and insert the character # at beginning of such lines.
          Find MatchCase RegExp "%^([0-9]+#^)"
          Replace All "#^1"
      //  Find a digit followed by character # and insert a line
      //  termination after the digit.
          Find MatchCase RegExp "^([0-9]^)#"
          Replace All "^1^p#"
      //  Now every number of a day is on a separate line with # at beginning.
      //  Run the following loop until the numbers of all days are sorted.
          Loop 0
      //  Find next line with character # at beginning. If not found, exit loop.
              Find MatchCase "#"
              IfNotFound
                  ExitLoop
              EndIf
      //  Move caret left to found character # and start selection mode.
              Key LEFT ARROW
              StartSelect
      //  Select everything up to a line not starting with character # (= next day).
              Find MatchCase RegExp Select "%[~#^p]"
              IfFound
      //  If such a line could be found, move the caret left to beginning of line
      //  while selection mode is still active. Now all number lines of a day are
      //  selected. Sort this block which results in discarding the selection and
      //  moving the caret to start of the first line of the block. So use another
      //  regular expression Find to move caret downwards to the first line of the
      //  next day (starts with a digit).
                  Key LEFT ARROW
                  EndSelect
                  SortAsc Numeric 2 -1 0 0 0 0 0 0
                  Find MatchCase RegExp "%[0-9]"
                  IfNotFound
                      ExitLoop
                  EndIf
              Else
      //  If there is no line not starting with character # anymore, this is the
      //  last day in the file. Select from start of first line with a number to
      //  end of line and sort this last block before exiting the loop. This is
      //  the typical exit. The other 2 exits are just for security in case the
      //  macro is executed on a wrong file.
                  SelectToBottom
                  SortAsc Numeric 2 -1 0 0 0 0 0 0
                  ExitLoop
              EndIf
          EndLoop
      //  Move caret to top of file and delete all inserted # from the file.
          Top
          Find MatchCase "#"
          Replace All ""
      Your second request can't be done with an UltraEdit macro. That requires an UltraEdit script with support for variables and arrays.

        Dec 23, 2011#3

        And here is the UltraEdit script for your second request with comments.

        Code: Select all

        function Numsort (a, b)  // Function for sorting the array of numbers.
        {
           return a - b;
        }
        
        if (UltraEdit.document.length > 0)  // Is any file opened in UltraEdit.
        {
           // Define the environment for the script.
           UltraEdit.insertMode();
           UltraEdit.columnModeOff();
           UltraEdit.activeDocument.hexOff();
           UltraEdit.perlReOn();
           // Make sure that also last line of file ends with a line termination.
           UltraEdit.activeDocument.bottom();
           if (UltraEdit.activeDocument.isColNumGt(1))
           {
              UltraEdit.activeDocument.insertLine();
              if (UltraEdit.activeDocument.isColNumGt(1))
              {
                 UltraEdit.activeDocument.deleteToStartOfLine();
              }
           }
           UltraEdit.activeDocument.top();   // Move caret to top of the file.
        
           // Define the parameters for a Perl regular expression search finding
           // 1 or more lines containing only space or tab delimited integers.
           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;
           }
        
           // Run the following loop as long as line(s) containing only
           // integer numbers with or without spaces/tabs are found.
           while (UltraEdit.activeDocument.findReplace.find("^(?:[ \\t\\d\\-]+\\r\\n)+"))
           {
              // The found lines are selected. Extract from the selected block
              // all integer numbers into an array of strings with each element
              // containing 1 number as string.
              var asNumbers = UltraEdit.activeDocument.selection.match(/-*\d+/g);
              // Create a second array of integers and convert the integer strings into
              // real integers. The integer array is next sorted according to the values.
              var nNumCount = asNumbers.length;
              if (nNumCount > 1)
              {
                 var anNumbers = new Array(nNumCount);
                 for (var nIndex = 0; nIndex < nNumCount; nIndex++)
                 {
                    anNumbers[nIndex] = parseInt(asNumbers[nIndex],10);
                 }
                 anNumbers.sort(Numsort);
                 // Create in memory a block with pairs of the found integers.
                 var sBlock = "";
                 for (var nFirst = 0; nFirst < (nNumCount - 1); nFirst++)
                 {
                    for (var nSecond = nFirst + 1; nSecond < nNumCount; nSecond++)
                    {
                       sBlock += anNumbers[nFirst].toString() + " " + anNumbers[nSecond].toString() + "\r\n";
                    }
                 }
                 // Write this block to the file overwritting the selection.
                 UltraEdit.activeDocument.write(sBlock);
              }
           }
        }
        And if you replace in above script the block

        Code: Select all

                 // Create in memory a block with pairs of the found integers.
                 var sBlock = "";
                 for (var nFirst = 0; nFirst < (nNumCount - 1); nFirst++)
                 {
                    for (var nSecond = nFirst + 1; nSecond < nNumCount; nSecond++)
                    {
                       sBlock += anNumbers[nFirst].toString() + " " + anNumbers[nSecond].toString() + "\r\n";
                    }
                 }
        by the block

        Code: Select all

                 // Create in memory a block with the sorted numbers.
                 var sBlock = "";
                 for (var nIndex = 0; nIndex < nNumCount; nIndex++)
                 {
                    sBlock += anNumbers[nIndex].toString() + "\r\n";
                 }
        you get a second script making the same as the macro above with a completely different method.

        12
        Basic UserBasic User
        12

          Dec 24, 2011#4

          Thank you for your macro and script, Mofi.