Use a Source File to Modify a Target

Use a Source File to Modify a Target

2
NewbieNewbie
2

    May 22, 2008#1

    I have an application where there is a space-delimited 3-column ascii file that is used to modify a second ascii file. The first 2 columns of the first file contain a column and row number used to locate the position of a value in the second file and the third row contains the value to be replaced.

    What is the best way to go about this? Is a conversion of the first file to a 2-dimensional array necessary?

    Regards,
    Gary

    6,686585
    Grand MasterGrand Master
    6,686585

      May 23, 2008#2

      No, it is not necessary as you can see below. But selecting a whole line, read it into a variable and then extract the row and column number and the new value is maybe faster then what I have done below.

      Code: Select all

      UltraEdit.insertMode();
      if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
      else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
      UltraEdit.ueReOn();
      UltraEdit.document[0].hexOff();  // space delimited file
      UltraEdit.document[1].hexOff();  // file to modify
      
      /* Make sure that last line of the space delimited file has
         a line ending to surely reach always the end of file. */
      UltraEdit.document[0].bottom();
      if (UltraEdit.document[0].isColNumGt(1)) {
         UltraEdit.document[0].insertLine();
      }
      UltraEdit.activeDocument.top();
      
      var Row=1;
      var Column=1;
      var NewVal="";
      
      while (UltraEdit.document[0].isEof() == false) {
         UltraEdit.document[0].selectWord();
         Column = UltraEdit.document[0].selection;
         UltraEdit.document[0].key("CTRL+RIGHT ARROW");
         UltraEdit.document[0].selectWord();
         Row = UltraEdit.document[0].selection;
         UltraEdit.document[0].key("CTRL+RIGHT ARROW");
         UltraEdit.document[0].selectWord();
         NewVal = UltraEdit.document[0].selection;
         // UltraEdit.messageBox("Debug: Column="+Column+", Row="+Row+", NewVal="+NewVal);
         UltraEdit.document[1].gotoLine(Number(Row),Number(Column));
         UltraEdit.document[1].selectWord();
         UltraEdit.document[1].write(NewVal);
         UltraEdit.document[0].key("DOWN ARROW");
         UltraEdit.document[0].key("HOME");
      }
      Best regards from an UC/UE/UES for Windows user from Austria

      2
      NewbieNewbie
      2

        May 26, 2008#3

        Mofi: Thanks for getting me started on this. Two questions I have are: 1. Is there a way to automate bringing up the Open File Dialog box to open the files or are we restricted to manually opening them before starting the script? The open command seems to indicate that you would need to hardcode the file/path name in quotes or transfer something from the clipboard.
        2. The search in the second file is more complicated than specifying a row/column value, since there is a preamble, so I would need to use a concatenated string variable to advance through the file. Again, for the findReplace command, it appears a variable can't be used.

        Thanks,
        Gary

        6,686585
        Grand MasterGrand Master
        6,686585

          May 27, 2008#4

          barkerg wrote:1. Is there a way to automate bringing up the Open File Dialog box to open the files or are we restricted to manually opening them before starting the script?
          Yes, use UltraEdit.open(""); and you will see the file open dialog. Of course you have to use this command twice and the script user must know which files to open. You should test in your script which files are already open when starting the script to ignore them and test on some file specific content which file the user has opened first and which one second to run the script correct independent if the user has specified first the file to modify and second the one with the values or vice versa.
          barkerg wrote:2. The search in the second file is more complicated than specifying a row/column value, since there is a preamble, so I would need to use a concatenated string variable to advance through the file.
          No problem with scripts. You can use a variable of type string in every function call for input parameters of type string. And there are the JavaScript core functions Number(StringVar) and String(NumberVar) to convert a string into a number variable and vice versa.
          Best regards from an UC/UE/UES for Windows user from Austria