Selecting number

Selecting number

2
NewbieNewbie
2

    Jul 27, 2007#1

    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.

    344
    MasterMaster
    344

      Jul 27, 2007#2

      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
      Normally using all newest english version incl. each hotfix. Win 10 64 bit

      262
      MasterMaster
      262

        Jul 27, 2007#3

        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);
        }
        //--------------------------------------

        6,681583
        Grand MasterGrand Master
        6,681583

          Jul 27, 2007#4

          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.
          Best regards from an UC/UE/UES for Windows user from Austria

          2
          NewbieNewbie
          2

            Jul 27, 2007#5

            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.