Replacement with loop...

Replacement with loop...

2
NewbieNewbie
2

    Oct 11, 2007#1

    Greetings!

    I'm a new user and I have not much experience in programming. What I want to do is a simple job but I haven't been able to figure it out on my own - it's probably a very easy thing for most users...

    Here is the file that I have;

    x323,x234,x932,x991
    x234,x323,x991
    x139,x234
    ..

    What I want to do is the following:

    for each line (there are about 400000 lines in this file), get the line number
    find the first word in each line
    replace all instances of this word in this entire file with the line number
    then go to the next line, repeat till the end of file

    Basically I am trying to replace all these names with serial numbers (same as the line number)

    Here is the code that I wrote;

    =================
    var index;
    for (index = 1; index < 4000; index++) {
    UltraEdit.activeDocument.gotoLine(index,1);
    UltraEdit.activeDocument.selectWord;
    var text = UltraEdit.activeDocument.selection;
    UltraEdit.activeDocument.findReplace.replace(text,index);
    }

    ============

    I guess I messed up "text" and "index"; but I still can't figure out how to make it work :(

    Thanks a lot!

    Rick

    6,681583
    Grand MasterGrand Master
    6,681583

      Oct 12, 2007#2

      Here is the script which should work for you. I have tested it on your small example with UE v13.10a+2.

      Code: Select all

      // Define working environment.
      UltraEdit.insertMode();
      if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
      else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
      UltraEdit.activeDocument.hexOff();
      UltraEdit.ueReOn();
      
      // Make sure last line of the file has a line termination.
      UltraEdit.activeDocument.bottom();
      if (UltraEdit.activeDocument.isColNumGt(1)) UltraEdit.activeDocument.insertLine();
      UltraEdit.activeDocument.top();
      
      // Define needed variables.
      var selword;  // String variable to hold the first word on a line.
      var linenr;   // Value variable to hold the line number of the current line.
      
      // Define the needed search and replace properties.
      UltraEdit.activeDocument.findReplace.matchWord=true;
      UltraEdit.activeDocument.findReplace.replaceAll=true;
      UltraEdit.activeDocument.findReplace.searchDown=true;
      UltraEdit.activeDocument.findReplace.matchCase=false
      UltraEdit.activeDocument.findReplace.preserveCase=false;
      UltraEdit.activeDocument.findReplace.regExp=false;
      UltraEdit.activeDocument.findReplace.replaceInAllOpen=false;
      UltraEdit.activeDocument.findReplace.selectText=false;
      
      // Run the replaces line by line till end of file is reached.
      while (!UltraEdit.activeDocument.isEof()) {
         // Get current line number.
         linenr = UltraEdit.activeDocument.currentLineNum;
         // Get the word at start of every line.
         UltraEdit.activeDocument.selectWord();
         selword = UltraEdit.activeDocument.selection;
         // Move cursor to top of the file.
         UltraEdit.activeDocument.top();
         /* Run the replace all with the selected word as
            search and the line number as replace string. */
         UltraEdit.activeDocument.findReplace.replace(selword, String(linenr));
         // Move the cursor one line below last line to column 1.
         UltraEdit.activeDocument.gotoLine(linenr+1,1);
      }
      
      // At the end move cursor back to top of the file.
      UltraEdit.activeDocument.top();
      Best regards from an UC/UE/UES for Windows user from Austria

      2
      NewbieNewbie
      2

        Oct 13, 2007#3

        Thanks a lot Mofi! I am running the code now and hopefully it would get through soon. I really appreciate your help.