9 Replies Latest reply on Oct 30, 2008 4:59 AM by (Dave_Saunders)

    JS Working with an array inside of an array

    John.Kordas Level 1
      I have a script set up for printing pages but would like to give the user the option to print a range of pages if then need to.

      Currently if you print a file you can select a page range 2,4,8,11-18,22 for example.

      I can put the numbers into an array as shown below:

      var numbers, mynumbers ;

      numbers = "1,5,6-9,11";

      var numbersarray = new Array;
      numbersarray = numbers.split(',');
      var mynumbers = numbersarray;
      var countarray = numbersarray.length;

      for(var myCounter = 0; myCounter < numbersarray.length; myCounter++){
      alert(mynumbers[myCounter]);
      }

      What would be the best way to split 6-9

      currently the array will return:

      mynumbers[0] => 1
      mynumbers[1] => 5
      mynumbers[2] => 6-9
      mynumbers[3] => 11

      Would it be possible to get:

      mynumbers[0] => 1
      mynumbers[1] => 5
      mynumbers[2] => 6
      mynumbers[3] => 7
      mynumbers[4] => 8
      mynumbers[5] => 9
      mynumbers[6] => 11
        • 1. Re: JS Working with an array inside of an array
          Harbs. Level 6
          How 'bout something like this?<br /><br />function DecomposeNumberRange(numberRange){<br />    var retVal=[];<br />    for(var i=0;i<numberRange.length;i++){<br />        numberRange[i]=numberRange[i].split("-");<br />        if(numberRange[i].length==1){<br />            retVal.push(Number(numberRange[i][0]));<br />            }<br />        else{<br />            var endNumber=Number(numberRange[i][1]);<br />            var startNumber = Number(numberRange[i][0]);<br />            while(startNumber<=endNumber){<br />                retVal.push(startNumber);<br />                startNumber++;<br />                }<br />            }<br />        }<br />    return retVal;<br />    }<br /><br /><br />Harbs
          • 2. Re: JS Working with an array inside of an array
            Peter Kahrel Adobe Community Professional & MVP
            John,

            Why do you want to unrange any page ranges? You can enter them in the range field in the print dialog in the UI, and in a script you can say this:

            myDoc.printPreferences.pageRange = "1,5,6-9,11";

            Peter
            • 3. Re: JS Working with an array inside of an array
              John.Kordas Level 1
              Thanks guys I'm a little closer.

              Peter I'm using an example Olav put together:
              Olav Kvern, "Need script for printing Postscript files (ID CS2)" #4, 19 Nov 2007 4:45 pm

              the reason is that his example allows you to output a document as single pages. To do this manually you need to send each page on its own. The script will do it in one hit. I've made some changes that allow the used to specify a single page if they need but would like to be able to send 1,3,5-6,9,12 for example.

              If I use myDoc.printPreferences.pageRange = "1,5,6-9,11"; it will create one pdf with the pages 1,5,6-9,11 inside it.

              Harbs when I tried your script it returned this in the console 1,NaN,5,NaN,6,NaN,9,NaN,1,1. It did give me an idea and I've come up with this.

              var numbers, mynumbers, mydnumbers ;

              numbers = "1,5,6-9,11";

              var numbersarray = new Array;
              numbersarray = numbers.split(',');
              var mynumbers = numbersarray;
              var countarray = numbersarray.length;

              for(var myCounter = 0; myCounter < numbersarray.length; myCounter++){

              //alert("length is "+mynumbers[myCounter].length);
              if(/-/.test(mynumbers[myCounter]) == false){
              alert(mynumbers[myCounter]);
              }

              if (mynumbers[myCounter].length >2 && /-/.test(mynumbers[myCounter])){

              alert("dash num are "+mynumbers[myCounter]);

              var dasharray = new Array;
              dasharray = mynumbers[myCounter].split('-');
              var dashnumbers = dasharray;
              //alert("dash "+dashnumbers);

              for (i=dashnumbers[0]; i < dashnumbers[1]; i++){

              var count = dashnumbers[0];

              alert(count);
              }
              count++;
              }
              }

              For some reason count sticks at 6 and count++ does not loop to 7, 8, then 9. Any suggestions?
              • 4. Re: JS Working with an array inside of an array
                John.Kordas Level 1
                OK this fixed the count++

                var dasharray = new Array;
                dasharray = mynumbers[myCounter].split('-');
                var dashnumbers = dasharray;
                var start = parseInt(dashnumbers[0]);
                var end = (parseInt(dashnumbers[1])+1);
                alert("dash "+end);

                for (i=start; i < end; i++){

                var count = i;

                alert(count);

                So I now get alerts of 1,5,6,7,8,9,11.

                Just need to put it in my script and hope it works.

                Cheers.
                • 5. Re: JS Working with an array inside of an array
                  Peter Kahrel Adobe Community Professional & MVP
                  John,

                  I thought you were sending pages to a printer, hadn't realised that it had to go to PDF as separate pages. Your script displays the following alerts in succession:

                  1
                  5
                  dash num are 6-9
                  dash 10
                  6
                  7
                  8
                  9
                  11

                  I use a script to combine index references, which (among other things) needs to unravel page ranges. This is the (slightly modified) function I use:

                  unranged_array = unrange ("1,5,6-11")
                  

                  function unrange (s)
                     {
                     return s.replace (/(\d+)-(\d+)/g, expand)
                     }

                  function expand ()
                     {
                     var expanded = [];
                     var start = Number (arguments[1]);
                     var stop = Number (arguments[2]);
                     for (var i = start; i <= stop; i++)
                        expanded.push (i);
                     return expanded
                     }


                  Peter
                  • 6. Re: JS Working with an array inside of an array
                    Harbs. Level 6
                    Peter,

                    Very nice code!

                    John, my function was meant to accept an array of pages or page ranges.
                    Not a string...

                    (It returns an expanded array as well...)

                    Harbs
                    • 7. Re: JS Working with an array inside of an array
                      Peter Kahrel Adobe Community Professional & MVP
                      Thanks, Harbs. This kind of stuff always seems like black magic to me, but when I close my eyes and type away, it sometimes works :)

                      Peter
                      • 8. Re: JS Working with an array inside of an array
                        John.Kordas Level 1
                        That's brilliant Peter,

                        I knew there had to be a better way of doing it.

                        Much appreciated.
                        • 9. Re: JS Working with an array inside of an array
                          Level 1
                          Peter,

                          This is the first time I've looked closely at what you posted here. That is a very instructive example.

                          Thank you very much.

                          Dave