Conjoining data from two open files

Help with writing and running scripts

Conjoining data from two open files

Postby dictdoc » Fri Apr 15, 2011 6:00 am

I am trying to generate NGrams for name analysis
I have two files open in UltraEdit. File one contains the basic NGram and file 2 the character that has to be added recursively to the NGram. The following example will make the case clear.

File 1
bb
bc
bd
be
bf

File 2
b
c

Expected output:
bbb
bcb
bdb
beb
bfb
bbc
bcc
bdc
bec
bfc

How do I write a macro or script which can generate out the list and store it in a new file?

Many thanks in advance
dictdoc
Basic User
Basic User
 
Posts: 18
Joined: Tue Jan 19, 2010 1:56 am

Re: Conjoining data from two open files

Postby Mofi » Fri Apr 15, 2011 12:52 pm

Although this could be done with a macro, I found it easier to do it with a script which makes everything in memory and therefore is extremly fast. Here is the script for that task.

Code: Select all
if (UltraEdit.document.length >= 2) {

   UltraEdit.insertMode();
   UltraEdit.columnModeOff();

   // Select everything in file 1 and load it into a string variable.
   // Then unselect content in file 1 and redo the same for file 2.
   UltraEdit.document[0].selectAll();
   if (UltraEdit.document[0].isSel()) {
      var sList1 = UltraEdit.document[0].selection;
      UltraEdit.document[0].top();
      UltraEdit.document[1].selectAll();
      if (UltraEdit.document[1].isSel()) {
         var sList2 = UltraEdit.document[1].selection;
         UltraEdit.document[1].top();

         // Check if first and second list end with a DOS line terminator.
         if (sList1.length < 2) sList1 += "\r\n";
         else if (sList1.substr(sList1.length-2,2) != "\r\n") sList1 += "\r\n";
         if (sList2.length < 2) sList2 += "\r\n";
         else if (sList2.substr(sList2.length-2,2) != "\r\n") sList2 += "\r\n";

         // List 2 currently stored in one long string must be split up into
         // an array of strings with deleting the last one which is here always
         // an empty string because entire list string always ends with \r\n.
         var asWords = sList2.split("\r\n");
         asWords.pop();

         // Build now the new list with inserting the current word
         // from list 2 at end of every line from list 1 in a loop.
         var sNewList = "";
         for (var nWord = 0; nWord < asWords.length; nWord++) {
            sNewList += sList1.replace(/\r\n/g,asWords[nWord]+"\r\n");
         }
         // Output the created new list into a new file.
         UltraEdit.newFile();
         UltraEdit.activeDocument.write(sNewList);
         UltraEdit.activeDocument.top();
      }
   }
}
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4049
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Conjoining data from two open files

Postby dictdoc » Fri Apr 15, 2011 10:44 pm

Dear Mofi,
Vielen dank. Works like a charm and created the corrrect amount of NGrams.
Many thanks once again.
dictdoc
Basic User
Basic User
 
Posts: 18
Joined: Tue Jan 19, 2010 1:56 am


Return to Scripts