-
1. Re: Reverse loop
Peter Kahrel Jun 28, 2015 2:21 PM (in response to Math Kay)Maybe you should tell us what you mean by 'reverse loop'.
-
2. Re: Reverse loop
BSKTCreation Jun 29, 2015 6:20 AM (in response to Math Kay)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 Jun 29, 2015 6:28 AM (in response to BSKTCreation)Hi,
Thank you so much for response. I will try and reply to you.
Thanks
Math