Script for creation of an array of strings with a zigzag pattern

Script for creation of an array of strings with a zigzag pattern

3
NewbieNewbie
3

    Nov 25, 2014#1

    Hi all,

    I am new on this board, recently started using UltraEdit.

    I want to change words using for loop, First Column, First Row, Second Column, Second Row and so on.

    Can anyone give me example?

    Code: Select all

    0xFFFFFF; 0x000000; 0x000000; 0x000000; 0x000000; 0x000000; 0x000000; 0x000000; 0x000000;
    0x000000; 0xFFFFFF; 0x000000; 0x000000; 0x000000; 0x000000; 0x000000; 0x000000; 0x000000;
    0x000000; 0x000000; 0xFFFFFF; 0x000000; 0x000000; 0x000000; 0x000000; 0x000000; 0x000000;
    0x000000; 0x000000; 0x000000; 0xFFFFFF; 0x000000; 0x000000; 0x000000; 0x000000; 0x000000;
    0x000000; 0x000000; 0x000000; 0x000000; 0xFFFFFF; 0x000000; 0x000000; 0x000000; 0x000000;
    0x000000; 0x000000; 0x000000; 0x000000; 0x000000; 0xFFFFFF; 0x000000; 0x000000; 0x000000;
    0x000000; 0x000000; 0x000000; 0x000000; 0x000000; 0x000000; 0xFFFFFF; 0x000000; 0x000000;
    0x000000; 0x000000; 0x000000; 0x000000; 0x000000; 0x000000; 0x000000; 0xFFFFFF; 0x000000;
    0x000000; 0x000000; 0x000000; 0x000000; 0x000000; 0x000000; 0x000000; 0x000000; 0xFFFFFF;
    0x000000; 0x000000; 0x000000; 0x000000; 0x000000; 0x000000; 0x000000; 0xFFFFFF; 0x000000;
    0x000000; 0x000000; 0x000000; 0x000000; 0x000000; 0x000000; 0xFFFFFF; 0x000000; 0x000000;
    0x000000; 0x000000; 0x000000; 0x000000; 0x000000; 0xFFFFFF; 0x000000; 0x000000; 0x000000;
    Best Regards,
    Inssof

    6,603548
    Grand MasterGrand Master
    6,603548

      Nov 26, 2014#2

      This is of course possible with a script. But there are some important facts missing in your question.
      1. How large is the file? Are we talking about some KB or hundreds of MB?
        That makes a big difference in coding the script.
      2. How many words separated by semicolons are in a line?
        More than 9 words might make the solution already much more difficult as Perl regular expression replaces with back-references cannot be used which would have been my first idea.
      3. Why looks line 10 of your (output?) example like line 8 and not like line 1? Should the replace work with a zigzag pattern?
      Best regards from an UC/UE/UES for Windows user from Austria

      3
      NewbieNewbie
      3

        Nov 27, 2014#3

        Hi Mofi,

        Thanks for your reply.

        I am electronic engineer. I want to pass this HEX numbers to central processing unit. This group of HEX numbers belongs to one array and we want to create thousands of arrays with different set and numbers.
        One array size we want to set from 90 to 1024 range. In one line we can have 9 words but then we need to create 100 lines for achieving 900 words.
        Mofi wrote:How large is the file? Are we talking about some KB or hundreds of MB?
        Size is going to 1 MB max. I have tested and found around 90 words we can save in 80 KB file.
        Mofi wrote:How many words separated by semicolons are in a line?
        180 words
        Mofi wrote:Should the replace work with a zigzag pattern?
        Looking for zigzag pattern.

        It will be great help if you give me a small example program with 9 words in one line with 9 rows.

        Again thanks for your kind reply. :)

        Best Regards,
        Inssof

        6,603548
        Grand MasterGrand Master
        6,603548

          Nov 27, 2014#4

          Okay, here is a script for producing text into a new file with the zigzag pattern you want.

          Code: Select all

          // Define environment for this script.
          UltraEdit.insertMode();
          if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
          else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
          
          var nRowsTotal = 100;   // Total number of data rows.
          var nWordsPerRow = 9;   // Total number of words per line.
          
          // Create an array of lines with walking 0xFFFFFFFF.
          var asDataLines = new Array(nWordsPerRow);
          for (var nDataLine = 0; nDataLine < asDataLines.length; nDataLine++)
          {
             var sLine = "";
             for (var nWord = 0; nWord < nWordsPerRow; nWord++)
             {
                sLine += (nWord != nDataLine) ? "0x00000000; " : "0xFFFFFFFF; "
             }
             asDataLines[nDataLine] = sLine.replace(/ $/,"\r\n");
          }
          
          // Use user clipboard 9 to create all rows in clipboard.
          UltraEdit.selectClipboard(9);
          UltraEdit.clearClipboard();
          
          var nWord = 0;        // Defines which word is next 0xFFFFFFF
          var nCountValue = 1;  // For counting upwards or downwards.
          nWordsPerRow--;       // Index of array starts with 0.  
          
          for (var nRow = 0; nRow < nRowsTotal; nRow++)
          {
             UltraEdit.clipboardContent += asDataLines[nWord];
             if (nWord == nWordsPerRow)
             {
                nCountValue = -1;
             }
             else if(nWord == 0)
             {
                nCountValue = 1;
             }
             nWord += nCountValue;
          }
          
          // Create a new ASCII file with DOS line terminators.
          UltraEdit.newFile();
          UltraEdit.activeDocument.unixMacToDos();
          UltraEdit.activeDocument.unicodeToASCII();
          
          // Paste the lines, move caret to top of file, clear clipboard 9 to free
          // memory, and switch back to system clipboard before exiting the script.
          UltraEdit.activeDocument.paste();
          UltraEdit.activeDocument.top();
          UltraEdit.clearClipboard();
          UltraEdit.selectClipboard(0);
          
          You can modify nRowsTotal and nWordsPerRow like you want. But take into account that nRowsTotal < nWordsPerRow does not make much sense for this pattern.

          Also the memory is limited, much more limited than your computer has in total free RAM on starting the script. An out of memory error will occur with nRowsTotal * nWordsPerRow being too high. In this case script execution must be terminated and UltraEdit exited and restarted.

          A method to create with a script a very large file can be seen at insert numerical sequence in large text file.
          Best regards from an UC/UE/UES for Windows user from Austria

          3
          NewbieNewbie
          3

            Nov 28, 2014#5

            Hi Mofi

            I have just tested provided script but its creating new blank file.

            Your provided script I have save in js.js file and executed on UltraEdit. :?

            Regards,
            Inssof

            6,603548
            Grand MasterGrand Master
            6,603548

              Nov 28, 2014#6

              All you need to do is copy my code into a new ASCII file, save it with a name you want like CreateArray.js in a directory with write permissions for you, and use Scripting - Run Active Script with this script file being active file. That's it.

              The script file can be even a UTF-8 or UTF-16 file with or without BOM since UE v21.30.0.1016.

              The script works for me on Windows XP x86 and Windows 7 x64 with UE v21.30.0.1016. Open output window before script execution and look if you get displayed in output window an error message on execution of the script.
              Best regards from an UC/UE/UES for Windows user from Austria