Function to mix lines of text in random order?

Help with writing and running scripts

Function to mix lines of text in random order?

Postby SGvnY7w0 » Fri May 30, 2008 1:49 pm

Hello,

Is there a function in UltraEdit to mix lines of text in a random order? By a line of text I mean a text string that ends with a line break.
SGvnY7w0
Newbie
 
Posts: 1
Joined: Fri May 30, 2008 1:47 pm

Re: Function to mix lines of text in random order?

Postby mjcarman » Fri May 30, 2008 2:07 pm

No. If your text is suitably random you might be able to specify start/end columns in the advanced sort options and get a crude pseudo-random effect. A better solution would be to write a script to shuffle lines based on the results of a rand() call.
User avatar
mjcarman
Power User
Power User
 
Posts: 123
Joined: Thu Feb 10, 2005 12:00 am

Re: Function to mix lines of text in random order?

Postby mjcarman » Fri May 30, 2008 3:36 pm

mjcarman wrote:A better solution would be to write a script to shuffle lines based on the results of a rand() call.

At least, it would be if UltraEdit's scripting engine supported rand(). :(
User avatar
mjcarman
Power User
Power User
 
Posts: 123
Joined: Thu Feb 10, 2005 12:00 am

Re: Function to mix lines of text in random order?

Postby mjcarman » Tue Jun 10, 2008 12:56 pm

I've learned that while there's no built-in rand() there is Math.random(). We can use that to build our own rand() making this possible after all.

Code: Select all
/* Fisher-Yates shuffle of file */
var i, j;  // line numbers
var a, b;  // line text

UltraEdit.activeDocument.bottom();
i = UltraEdit.activeDocument.currentLineNum;

while (--i) {
   j = rand(i + 1);

   UltraEdit.activeDocument.gotoLine(i, 0);
   UltraEdit.activeDocument.selectLine();
   a = UltraEdit.activeDocument.selection;

   UltraEdit.activeDocument.gotoLine(j, 0);
   UltraEdit.activeDocument.selectLine();
   b = UltraEdit.activeDocument.selection;

   UltraEdit.activeDocument.deleteLine();
   UltraEdit.activeDocument.write(a);

   UltraEdit.activeDocument.gotoLine(i, 0);
   UltraEdit.activeDocument.deleteLine();
   UltraEdit.activeDocument.write(b);

}

// Generate a random integer in the range [0, N)
function rand(n) {
   return Math.floor(Math.random() * n);
}
User avatar
mjcarman
Power User
Power User
 
Posts: 123
Joined: Thu Feb 10, 2005 12:00 am

Re: Function to mix lines of text in random order?

Postby Doug E » Fri Nov 07, 2008 8:13 pm

mjcarman

I had heard of UltraEdit as I was looking for something that would perform this function. I was disappointed to see it wasn't included but am now happy to have found your script. It seems to work fine. It is the reason I will be purchasing UltraEdit. Thanks.
Doug E
Newbie
 
Posts: 5
Joined: Fri Nov 07, 2008 8:09 pm


Return to Scripts