Script to change filrewall rules

Hi,

I am hoping to achieve the following.
I want to go through all the ips in ip adresses for a certain range,say 192.168.1.1/32,and check if there are certain firewall rules.If not,add the rule,else do nothing.

I’m fine with the firewall commands and so om,just need some help on the scripting side.How will i,in my for loop,get the address back and use this in my ip firewatt command?

say i have a bunch of /30’s in the 192.168.1 range,i.e a have a 192.168.1.1/30,a 192.168.1.5/30,etc.I want to check if a rule is added for 192.168.1.2 and 192.168.1.6 in this case and if not add the rule.
i.e somtehing like this

:for $i in ip address print                     //how can i just select a certain range(all starting with 192.168.1.1),and how will i store this in $i
:if ip firewall nat [find src-address=$i]  //i need to search for an ip in the range(192.168.1.2 as opposed to 192.168.1.1/30) $i
//do nothing
:else
ip firewall nat add rule                                               //no problem here

Also,how can i debug mikrotik scripts,i.e print some output to check the control flow,etc.

Thanks for the help