Tapatalk

Function to mix lines of text in random order?

Function to mix lines of text in random order?

1
NewbieNewbie
1

    May 30, 2008#1

    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.

    119
    Power UserPower User
    119

      May 30, 2008#2

      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.

        May 30, 2008#3

        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(). :(

          Jun 10, 2008#4

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

          5
          NewbieNewbie
          5

            Nov 08, 2008#5

            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.