Need Help for better loop code

To help others interested in the original version of the script, it's one of the scripts in this post:

(And, as Sasa says, it sets all the tags at once instead of one at a time.)