Arrange numbers and lists the possible cases occurred?

Help with writing and running scripts

Arrange numbers and lists the possible cases occurred?

Postby bitmap007 » Fri Dec 23, 2011 8:24 am

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
bitmap007
Basic User
Basic User
 
Posts: 14
Joined: Tue Jun 14, 2011 12:15 am

Re: Arrange numbers and lists the possible cases occurred?

Postby Mofi » Fri Dec 23, 2011 9:48 am

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.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3936
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Arrange numbers and lists the possible cases occurred?

Postby Mofi » Fri Dec 23, 2011 10:46 am

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.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3936
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Arrange numbers and lists the possible cases occurred?

Postby bitmap007 » Sat Dec 24, 2011 5:59 am

Thank you for your macro and script, Mofi.
bitmap007
Basic User
Basic User
 
Posts: 14
Joined: Tue Jun 14, 2011 12:15 am

Re: Arrange numbers and lists the possible cases occurred?

Postby bitmap007 » Tue Jan 01, 2013 4:56 am

Hi Mofi,

I am using the macro as you posted above and there is an error like this:

Microsoft Visual C++ Runtime Library
Runtime error!
Program: C...
R6002....


I don't know about this error. Please help me fix it. Thank you so much. Sorry about my English. I'm using UltraEdit v18.20.
bitmap007
Basic User
Basic User
 
Posts: 14
Joined: Tue Jun 14, 2011 12:15 am

Re: Arrange numbers and lists the possible cases occurred?

Postby Mofi » Tue Jan 01, 2013 6:32 am

I created with UE v18.20.0.1028 an UltraEdit macro with the code in my first post according to the steps posted for example at would like to hide comments.

Next I copied the 3 lines in your first post into a new ASCII file with DOS line terminators and saved the file as Test.tmp.

Then I executed the just created macro on file Test.tmp using once Macro - Play Again and there was no problem. The macro created the correct output.

Alternatively you can also use Macro - Play Any/Multiple Times with number of times to repeat macro set to 1 and play macro to end of file not checked, or by double clicking on the name of the macro in the macro list opened with View - Views/Lists - Macro List.

I have never seen a Microsoft Visual C++ runtime error message on executing an UltraEdit macro with UltraEdit. If possible pack the *.mac file containing the macro and a file on which execution of the macro results in the error message with ZIP or RAR and upload the archive file as attachment (max. 250 KB) to your next post. Perhaps I can reproduce the problem with the 2 files also on my computer and can further analyze the problem. However, the macro code is correct and if there is really a problem on execution of the UltraEdit macro, only the developers of IDM can fix it.

You can use instead of the macro also a script, see my second post here.

By the way: How large is the file on which you run the macro? The macro is using a numeric sort which may fail if the file is really large.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3936
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Arrange numbers and lists the possible cases occurred?

Postby bitmap007 » Tue Jan 01, 2013 8:20 am

here is the error. i think i is not macro error. :(
Attachments
error.rar
you can see it
(45.12 KiB) Downloaded 5 times
bitmap007
Basic User
Basic User
 
Posts: 14
Joined: Tue Jun 14, 2011 12:15 am

Re: Arrange numbers and lists the possible cases occurred?

Postby Mofi » Wed Jan 02, 2013 5:11 am

Lots of forum topics about this error message can be found with a WWW search engine.

In most of them the solution was to run an anti-virus application to remove the virus causing this error.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3936
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Scripts