3 Replies Latest reply on Jun 29, 2015 6:28 AM by Math Kay

    Reverse loop

    Math Kay Level 1

      Hi  Legends,

       

        Could anybody please explain about reverse loop in indesign scripting in detail. Please explain with example. It would be better and helpful to understand.

       

      Regards

      Math Kay

        • 1. Re: Reverse loop
          Peter Kahrel Adobe Community Professional & MVP

          Maybe you should tell us what you mean by 'reverse loop'.

          • 2. Re: Reverse loop
            BSKTCreation Level 2

            Hi Math,

             

            While I'm no legend I will try and explain a basic example of where I use a negative/reverse loop.

             

            In a positive loop deleting items that are part of the iteration count will cause a iteration mismatch and could cause unintended results. When I delete items that are part of the iteration count I use a negative loop so the iteration count remains intact.

             

            Take this code, if you run this it will delete all rows in table[0] that have a empty cell[0] of a row.

             

            var myTable = app.documents[0].stories.everyItem().tables.everyItem().getElements();
            
            
            for (var i = myTable[0].rows.length - 1; i >= 0; i--) {
                if (myTable[0].rows[i].cells[0].contents == "") {
                    myTable[0].rows[i].remove();
                    }
                }
            
            
            

             

             

            This code, on the other hand may not delete all the empty rows because as a row is deleted it effectively leap frogs the next row as it's been promoted up the order.

             

            var myTable = app.documents[0].stories.everyItem().tables.everyItem().getElements();
            
            
            for (var i = 0; i <= myTable[0].rows.length - 1; i++) {
                if (myTable[0].rows[i].cells[0].contents == "") {
                    myTable[0].rows[i].remove();
                    }
                }
            
            
            

             

             

            Try it out and see what happens. I hope this helps.

             

            Brett

            • 3. Re: Reverse loop
              Math Kay Level 1

              Hi,

               

              Thank you so much for response. I will try and reply to you.

               

              Thanks

              Math