Filtering numbers

Help with writing and running scripts

Filtering numbers

Postby Gradius » Wed Jan 09, 2013 2:27 pm

Hi,

I need a simple script where it filter numbers.

Input (yes, there are tabs instead spaces):
Code: Select all
1   3   7   9   10   17   21   22   29   30   32   36   60   67   69   70   73   77   98   100
1   10   11   12   13   16   22   29   45   46   48   49   51   57   59   63   73   78   91   94

Output needs to be ('tabs' were converted into 'spaces' and 100 into 00):
Code: Select all
01 03 07 09 10 17 21 22 29 30 32 36 60 67 69 70 73 77 98 00
01 10 11 12 13 16 22 29 45 46 48 49 51 57 59 63 73 78 91 94

Numbers 1, 2, 3, 4, 5, 6, 7, 8 and 9, needs to becomes 01, 02, 03, 04, 05, 06, 07, 08 and 09 too.

Thank you!
Gradius
Newbie
 
Posts: 8
Joined: Thu Feb 02, 2012 5:52 pm

Re: Filtering numbers

Postby Mofi » Thu Jan 10, 2013 1:36 am

Good description of what should be done by the script and therefore easy to write the script.

Code: Select all
// Run this script only when at least 1 file is opened.
if (UltraEdit.document.length > 0)
{
   // Define environment for script.
   UltraEdit.insertMode();
   UltraEdit.columnModeOff();
   UltraEdit.ueReOn();

   // Set caret to top of file and remove spaces/tabs at end of all lines.
   UltraEdit.activeDocument.top();
   UltraEdit.activeDocument.trimTrailingSpaces();

   // Define the parameters for the replaces.
   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;
   }
   UltraEdit.activeDocument.findReplace.preserveCase=false;
   UltraEdit.activeDocument.findReplace.replaceAll=true;
   UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;

   // Replace all sequences of tabs/spaces by a single space.
   UltraEdit.activeDocument.findReplace.replace("[ ^t]+", " ");

   // Truncate numbers with more than 2 digits using a tagged regular expression.
   UltraEdit.activeDocument.findReplace.replace("[0-9]+^([0-9][0-9]^)", "^1");

   // Insert a leading zero on those numbers having only 1 digit.
   UltraEdit.activeDocument.findReplace.matchWord=true;
   UltraEdit.activeDocument.findReplace.replace("^([0-9]^)", "0^1");
}
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3937
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Filtering numbers

Postby Gradius » Sat Jan 12, 2013 2:32 pm

I did as programmer's mind oriented. :o

Another script I need is "Set Selection" with no "begin" and "end" range input as in Edit > Select Range.

"Begin" will be where the cursor is on at present position. And "end" would be always the end of file (EOF).

So I would just need to select Column (start) and Column (end).

Thanks once again!
Gradius
Newbie
 
Posts: 8
Joined: Thu Feb 02, 2012 5:52 pm

Re: Expand selection to a rectangular selection to end of file

Postby Mofi » Sat Jan 12, 2013 4:05 pm

I'm not sure if I have understood completely what you want next.

Why not using Select Range to make a rectangular selection from current caret position to column X on last line of the file?

Let's say the caret is at column 20 on line 15 and the Select Range dialog is opened (by hotkey). As the beginning position is already right pressing twice TAB selects the number for the end line. Well, you most likely don't know it. But that does not matter as you can simply enter for example 9999999 to create a rectangular selection to column X on last line if the file has not more than 9999999 lines. Next you press once more TAB and enter the end column. Now hitting RETURN and the selection is made.

However, here is a script which assumes that there is a selection in current line marking begin and end column which should be expanded in column editing mode as rectangular selection to last line of the file.

Code: Select all
// Run this script only when at least 1 file is opened.
if (UltraEdit.document.length > 0)
{
   // There must be a selection in current line only with caret blinking
   // on end of the selection. It is important for correct working of the
   // script that the selection does not end beyond end of the current line.
   if (UltraEdit.activeDocument.isSel()) {
      // Define environment for script.
      UltraEdit.insertMode();
      UltraEdit.columnModeOff();
      // Get caret position at end of the selection.
      var nBeginLine = UltraEdit.activeDocument.currentLineNum;
      var nEndCol = UltraEdit.activeDocument.currentColumnNum;
      // The next line is needed only for UE prior v16.00 and UES prior v10.00.
      if (typeof(UltraEdit.activeDocumentIdx) == "undefined") nEndCol++;
      // Calculate the column at beginning of the selection by simply
      // subtracting the number of characters of the selection.
      var nBeginCol = nEndCol - UltraEdit.activeDocument.selection.length;
      // This results definitely in a wrong column number if a tab is included.
      if (UltraEdit.activeDocument.selection.indexOf('\t') >= 0) {
         // In case of a tab being included in the selection, cut the selection
         // to clipboard 9, get the column number at beginning of the selection
         // and paste the selection back.
         UltraEdit.selectClipboard(9);
         UltraEdit.activeDocument.cut();
         nBeginCol = UltraEdit.activeDocument.currentColumnNum;
         if (typeof(UltraEdit.activeDocumentIdx) == "undefined") nBeginCol++;
         UltraEdit.activeDocument.paste();
         UltraEdit.clearClipboard();
         UltraEdit.selectClipboard(0);
      }
      // Go to end of the file to get its line number.
      UltraEdit.activeDocument.bottom();
      var nEndLine = UltraEdit.activeDocument.currentLineNum;
      // If last line of file ends with a line termination, subtract 1
      // from the line number to get real line number of last line.
      if (UltraEdit.activeDocument.isColNum(1)) nEndLine--;
      // Go back (hopefully right) beginning of the initial selection.
      UltraEdit.activeDocument.gotoLine(nBeginLine,nBeginCol);
      // Turn on column editing mode and make now a rectangular
      // selection to initial end column in active line in last line.
      UltraEdit.columnModeOn();
      UltraEdit.activeDocument.gotoLineSelect(nEndLine,nEndCol);
   }
}

Please note that this script works only as designed if the current selection does not spread over multiple lines. And the selection must also end before end of active line. So the result is wrong if a selection is made (in already enabled column editing mode) beyond end of active line.

Also tabs within the selection makes it more difficult to determine starting column. A workaround is needed in this case which results in creating 2 undo steps.

And last if the selection in current line ends at a column greater the number of columns on last line, the selection is also not correct if the configuration setting Allow positioning beyond line end is not enabled.

Making a rectangular selection without command Select Range depends on many variables: the configuration setting Allow positioning beyond line end, the existence of the termination of the last line, the number of columns on last line, the number of columns in active line, and perhaps more. So it is very hard to write general scripts for making a rectangular selection working in all cases. For this reason I avoid making rectangular selections in macros and scripts as much as possible and use the column editing commands or regular expression replaces to reformat a rectangular block.

Just one more note:

Command Select Range with a too high end line number makes always a rectangular selection to end column on the line the caret is blinking when moving caret to end of file with Ctrl+End. This can be beyond last line of file if the last line ends with a line termination. The script above expands the selection to last line of file before last line termination if the last line ends with a line termination (and has enough characters as on active line).
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 3937
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Scripts