Selecting number

Help with writing and running scripts

Selecting number

Postby thesmith » Fri Jul 27, 2007 3:49 am

Hi all,
I have a problem that is driving me nuts... I am trying to select text that sits between 2 token characters.

The following code illustrates my problem.

Code: Select all
//--------------------------------------
UltraEdit.outputWindow.showWindow(true);
UltraEdit.outputWindow.clear();

// start at top
UltraEdit.activeDocument.top();

// set search values
UltraEdit.activeDocument.findReplace.matchWord = false;

// loop to end of file
var done = false;
while (!UltraEdit.activeDocument.isEof() && !done)
{
   // find the start
   UltraEdit.activeDocument.findReplace.find("1");
   if (UltraEdit.activeDocument.isFound())
     {
        // found, now start the selection and then find the last char
        UltraEdit.activeDocument.startSelect();
        UltraEdit.activeDocument.findReplace.find("9");
        if (UltraEdit.activeDocument.isFound())
        {
           // ok, have that, now grab the text
           UltraEdit.activeDocument.endSelect();
           var s = UltraEdit.activeDocument.selection;
           UltraEdit.outputWindow.write(s);
        }
        else
        {
           // done
           done = true;
        }
   }
   else
   {
      // done
        done = true;
   }
}
//--------------------------------------


Now if this is run over the following text :-
0012345678900
0012345678900
0012345678900
0012345678900
0012345678900
0012345678900

The result output is:-
9
9
9
9
9
9
Script succeeded.

Why is just the "9" being returned? I would have thought that "123456789" would have be returned. Maybe I'm missing something?

Thanks
Anthony.
User avatar
thesmith
Newbie
 
Posts: 2
Joined: Tue Jul 03, 2007 11:00 pm

Re: Selecting number

Postby Bego » Fri Jul 27, 2007 4:52 am

Hi,

sry, I am low on time, but why don't you do a regular expression search (perl style) with this expression:
Code: Select all
[1].*[9]


It should get you directly to the target. It finds everything between a 1 and a 9. Just do a loop and you are done.

rds Bego
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: Selecting number

Postby jorrasdk » Fri Jul 27, 2007 6:30 am

Find automatically select what it finds and thus unselects what have been selected beforehand. That's why only what is found by the last find for '9' is selected.

So going with Begos suggestion for a regex it could look something like this:

Code: Select all
//--------------------------------------
UltraEdit.outputWindow.showWindow(true);
UltraEdit.outputWindow.clear();

// start at top
UltraEdit.activeDocument.top();

// set search values
UltraEdit.activeDocument.findReplace.matchWord = false;
// use regex
UltraEdit.ueReOn();
UltraEdit.activeDocument.findReplace.regExp = true;

// loop to end of file
while (UltraEdit.activeDocument.findReplace.find("[1]?*[9]"))
{
   var s = UltraEdit.activeDocument.selection;
   UltraEdit.outputWindow.write(s);
}
//--------------------------------------
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark

Re: Selecting number

Postby Mofi » Fri Jul 27, 2007 6:31 am

Best is to use Bego's regular expression search string.

Your version does not work because every find always starts a new selection. In the macro environment there is the Find option Select which when used expands the current selection to end of found string or if there is currently no selection a selection is created from current cursor position to end of found string. But that Find only option is not available in the script environment.

Extra hint: To exit a loop you can use the command break. Then no variable done is needed anymore because you simply can write else break;
The break command always exits only the current loop. So be carefully when using it in nested loops (loops within a loop) in the inner loop.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4066
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Selecting number

Postby thesmith » Fri Jul 27, 2007 8:55 am

Thanks All,

I thought that may have been the problem, however as a matter of course I thought I would ask.

Mofi don't stress, I would normally use break to bail from a loop... :wink:
However, this code was pulled from a "bigger picture" and then quickly edited to illustrate a point.
User avatar
thesmith
Newbie
 
Posts: 2
Joined: Tue Jul 03, 2007 11:00 pm


Return to Scripts